Directshow中使用IAMStreamConfig接口改变摄像头分辨率

来源:互联网 发布:学生会网络部工作总结 编辑:程序博客网 时间:2024/06/10 03:09

Directshow中使用IAMStreamConfig接口改变摄像头分辨率 

【转自】http://blog.csdn.net/yanqiao1986/archive/2010/04/02/5445486.aspx

实验室的江同学在一个有关摄像头的小项目中用到Directshow在.net平台上的封装DshowNet,该项目运行时在显示视频之前都会弹出一个视频属性配置窗口要求手动配置如分辨率等视频属性。江同学要求能够去除这个弹出窗口而在代码中设置分辨率等属性。

于是定位到源码当中的ShowCapPinDialog函数

 

view plaincopy to clipboardprint?
  1. public static bool ShowCapPinDialog( ICaptureGraphBuilder2 bld, IBaseFilter flt, IntPtr hwnd )  
  2. {  
  3.           int hr;  
  4.           object comObj = null;  
  5.           ISpecifyPropertyPages spec = null;  
  6.           DsCAUUID cauuid = new DsCAUUID();  
  7.   
  8.           try  
  9.           {  
  10.               Guid cat = PinCategory.Capture;  
  11.               Guid type = MediaType.Interleaved;  
  12.               Guid iid = typeof(IAMStreamConfig).GUID;  
  13.               hr = bld.FindInterface(ref cat, ref type, flt, ref iid, out comObj);  
  14.               if (hr != 0)  
  15.               {  
  16.                   type = MediaType.Video;  
  17.                   hr = bld.FindInterface(ref cat, ref type, flt, ref iid, out comObj);  
  18.                   if (hr != 0)  
  19.                       return false;  
  20.               }  
  21.   
  22.               spec = comObj as ISpecifyPropertyPages;  
  23.               if (spec == null)  
  24.                   return false;  
  25.   
  26.               hr = spec.GetPages(out cauuid);  
  27.               hr = OleCreatePropertyFrame(hwnd, 30, 30, null, 1,  
  28.                       ref comObj, cauuid.cElems, cauuid.pElems, 0, 0, IntPtr.Zero);  
  29.               return true;  
  30.           }  
  31.           catch (Exception ee)  
  32.           {  
  33.               Trace.WriteLine("!Ds.NET: ShowCapPinDialog " + ee.Message);  
  34.               return false;  
  35.           }  
  36.           finally  
  37.           {  
  38.               if (cauuid.pElems != IntPtr.Zero)  
  39.                   Marshal.FreeCoTaskMem(cauuid.pElems);  
  40.   
  41.               spec = null;  
  42.               if (comObj != null)  
  43.                   Marshal.ReleaseComObject(comObj); comObj = null;  
  44.           }  
  45.   
  46.   
  47.         }  

 

在改源码中弹出的窗口是IAMStreamConfig的属性页。

于是想想使用IAMStreamConfig接口即能设定视频的分辨率。

搜了下发现http://topic.csdn.net/u/20080827/17/8b2dcced-316f-4b9d-80f6-e4776931ec73.html这个帖子中涉及到这个问题。12楼大虾的回帖很有参考价值。

因为摄像头只能支持固定的几种分辨率,所以必须枚举该摄像头支持的几种分辨率再判断该设置成哪种。

修改后的代码如下。

 

view plaincopy to clipboardprint?
  1. public static bool ShowCapPinDialog( ICaptureGraphBuilder2 bld, IBaseFilter flt, IntPtr hwnd )  
  2. {  
  3.           int hr;  
  4.           object comObj = null;  
  5.           IAMStreamConfig pamsc = null;  
  6.   
  7.           try  
  8.           {  
  9.               Guid cat = PinCategory.Capture;  
  10.               Guid type = MediaType.Interleaved;  
  11.               Guid iid = typeof(IAMStreamConfig).GUID;  
  12.               hr = bld.FindInterface(ref cat, ref type, flt, ref iid, out comObj);  
  13.   
  14.               if (hr != 0)  
  15.               {  
  16.                   type = MediaType.Video;  
  17.                   hr = bld.FindInterface(ref cat, ref type, flt, ref iid, out comObj);  
  18.                   if (hr != 0)  
  19.                       return false;  
  20.               }  
  21.   
  22.               pamsc = comObj as IAMStreamConfig;  
  23.               if (pamsc == null)  
  24.                   return false;  
  25.   
  26.               int count;  
  27.               int size;  
  28.               hr = pamsc.GetNumberOfCapabilities(out count, out size);  
  29.               IntPtr pt = Marshal.AllocHGlobal(128);  
  30.   
  31.               for (int iFormat = 0; iFormat < count; iFormat++)  
  32.               {  
  33.                   AMMediaType ammtp = new AMMediaType();  
  34.                   hr = pamsc.GetStreamCaps(iFormat, out ammtp, pt);  
  35.   
  36.                   if (hr == 0)  
  37.                   {  
  38.                       VideoInfoHeader pvihdr = new VideoInfoHeader();  
  39.                       Marshal.PtrToStructure(ammtp.formatPtr, pvihdr);  
  40.                       if (pvihdr.BmiHeader.Width == 176)  
  41.                       {  
  42.                           pvihdr.AvgTimePerFrame = 10000000 / 15;  
  43.                           hr = pamsc.SetFormat(ammtp);  
  44.                           break;  
  45.                       }  
  46.                   }  
  47.               }  
  48.   
  49.               return true;  
  50.           }  
  51.           catch (Exception ee)  
  52.           {  
  53.               Trace.WriteLine("!Ds.NET: ShowCapPinDialog " + ee.Message);  
  54.               return false;  
  55.           }  
  56.           finally  
  57.           {  
  58.               pamsc = null;  
  59.               if (comObj != null)  
  60.                   Marshal.ReleaseComObject(comObj);  
  61.               comObj = null;  
  62.           }  
  63. }  

 

上面代码简单描述了IAMStreamConfig接口的用法,最后将摄像头属性设置为分辨率176*144的模式。

发表于 @ 2010年04月02日

原创粉丝点击