首页 > 代码库 > DirectShow .Net 实现视频

DirectShow .Net 实现视频

 DirectShow .Net 实现视频

 

  1 1、获取视频采集设备IBaseFilter接口对象的方法
  2 
  3 //获取所有视频设备名称
  4 public ArrayList GetVideoInputDevice()
  5       { return GetDeviceCollection(FilterCategory.VideoInputDevice);}
  6 private ArrayList GetDeviceCollection(Guid DeviceType)
  7       {
  8           ArrayList returnString = new ArrayList();
  9           foreach (DsDevice ds in DsDevice.GetDevicesOfCat(DeviceType))
 10           {
 11               returnString.Add(ds.Name);
 12           }
 13           return returnString;
 14       }
 15 
 16 //通过获取到的视频设备名称设置采集设备的IBaseFilter对象
 17  public bool SetVideoInputDevice(string VideoInputDeviceName)
 18       {    //创建视频输入设备接口
 19           theVideoDevice = CreateFilter(FilterCategory.VideoInputDevice, VideoInputDeviceName); 
 20       }
 21 //通过过滤器类型和过滤器名称获取IBaseFilter接口
 22 private IBaseFilter CreateFilter(Guid category, string friendlyname)
 23       {
 24           object source = null;
 25           Guid iid = typeof(IBaseFilter).GUID;
 26           foreach (DsDevice device in DsDevice.GetDevicesOfCat(category))
 27           {
 28               if (device.Name.CompareTo(friendlyname) == 0)
 29               {
 30                   device.Mon.BindToObject(null, null, ref iid, out source);
 31                   break;
 32               }
 33           }
 34 
 35           return (IBaseFilter)source;
 36       }
 37 
 38 2、初始化基本的接口对象 
 39 
 40 private void InitInterfaces()
 41       {
 42           int hr = 0;
 43 
 44           // 获取IGraphBuilder接口对象
 45           this.m_graphBuilder = (IGraphBuilder)new FilterGraph();
 46           //获取ICaptureGraphBuilder2接口对象
 47           this.m_captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
 48           //获取m_graphBuilder 接口对象的IMediaControl对象
 49           this.m_mediaControl = (IMediaControl)this.m_graphBuilder;
 50           //获取m_graphBuilder 接口对象的IVideoWindow对象
 51           this.m_videoWindow = (IVideoWindow)this.m_graphBuilder;
 52           //获取m_graphBuilder 接口对象的IMediaEventEx对象
 53           this.m_mediaEventEx = (IMediaEventEx)this.m_graphBuilder;
 54           //设置ICaptureGraphBuilder2的IGraphBuilder接口对象为当前对象
 55           hr = this.m_captureGraphBuilder.SetFiltergraph(this.m_graphBuilder);
 56           DsError.ThrowExceptionForHR(hr);
 57           //注册事件到应用程序的窗体上
 58           hr = this.m_mediaEventEx.SetNotifyWindow(this.hwnPropertyPageOwner, WM_GRAPHNOTIFY, IntPtr.Zero);
 59           DsError.ThrowExceptionForHR(hr);
 60       }
 61 
 62  
 63 
 64  
 65 
 66   
 67 
 68 3、开始视频预览
 69 public void VideoPreview()
 70        try
 71       {
 72 
 73           int hr = 0;                  
 74           hr = this.m_graphBuilder.AddFilter(theVideoDevice, "Video Capture");
 75           DsError.ThrowExceptionForHR(hr);
 76 
 77           // 通过theVideoDevice(IBaseFilter)视频接口对象的Preview Pin预览
 78           hr = this.m_captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, theVideoDevice, null, null);
 79           DsError.ThrowExceptionForHR(hr);
 80 
 81            //插入SampleGrabber 
 82           m_pinStill = DsFindPin.ByCategory(theVideoDevice, PinCategory.Still, 0);
 83 
 84           if (m_pinStill == null)
 85               {
 86                   m_pinStill = DsFindPin.ByCategory(theVideoDevice, PinCategory.Capture, 0);
 87               }
 88 
 89 
 90               // 获取theVideoDevice的IAMVideoControl对象,对于具有Still Pin的对象可以获到,采集设备不具备Still Pin,那么该对象将为Null
 91               m_VidControl = theVideoDevice as IAMVideoControl;
 92 
 93               // 设置采集视频参数
 94               if (this.videoHeight + this.videoWidth + this.videoStride > 0)
 95               {
 96                   SetConfigParms(m_pinStill, this.videoWidth, this.videoHeight, 24);
 97               }
 98 
 99              //开始拍照功能所需的接口对象
100               // 获得SampleGrabber对象接口
101               sampGrabber = new SampleGrabber() as ISampleGrabber;
102 
103               // 配置sample grabber
104               baseGrabFlt = sampGrabber as IBaseFilter;
105               ConfigureSampleGrabber(sampGrabber);
106 
107               // 将sample grabber添加到图形过滤器中
108               hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
109               DsError.ThrowExceptionForHR(hr);
110 
111               //通过渲染将采集设备的相关输出Pin与sample grabber对象的输入Pin连接起来
112               //如果采集设备提供Still Pin,则通过Still Pin连接,否则直接使用Capture Pin连接
113               if (m_VidControl!=null)
114               {
115                   hr = this.m_captureGraphBuilder.RenderStream(PinCategory.Still, MediaType.Video, theVideoDevice, null, baseGrabFlt);
116                   DsError.ThrowExceptionForHR(hr);
117 
118               }
119               else
120               {
121                   hr = this.m_captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, theVideoDevice, null, baseGrabFlt);
122                   DsError.ThrowExceptionForHR(hr);
123               }
124             //设置抓取图片相关参数
125               SaveSizeInfo(sampGrabber);
126             //拍照功能所需的接口对象添加结束
127 
128 
129           // 开始将视频窗口绑定到主窗体上
130           hr = this.m_videoWindow.put_Owner(this.hwnVideoWindowOwner);
131           DsError.ThrowExceptionForHR(hr);
132 
133           hr = this.m_videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);
134           DsError.ThrowExceptionForHR(hr);
135 
136           if (this.m_videoWindow != null)
137           {
138               this.m_videoWindow.SetWindowPosition(0, 0, this.videoWidth, this.videoHeight);
139           }
140 
141 
142           hr = this.m_videoWindow.put_Visible(OABool.True);
143           DsError.ThrowExceptionForHR(hr);
144 
145           // 开始预览采集设备采集到的视频
146           hr = this.m_mediaControl.Run();
147 
148           DsError.ThrowExceptionForHR(hr);
149           m_IsPreview = true;
150       }
151       catch
152           {
153           m_IsPreview = false;
154           throw new Exception("VideoPreview函数出现异常,视频预览失败!");
155         
156       }
157       }

 

DirectShow .Net 实现视频