首页 > 代码库 > DirectShowLib directshownet 视频

DirectShowLib directshownet 视频

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

 

DirectShowLib directshownet 视频