收藏 分享(赏)

XP下采用DirectShow采集摄像头.doc

上传人:精品资料 文档编号:7663901 上传时间:2019-05-23 格式:DOC 页数:20 大小:225.50KB
下载 相关 举报
XP下采用DirectShow采集摄像头.doc_第1页
第1页 / 共20页
XP下采用DirectShow采集摄像头.doc_第2页
第2页 / 共20页
XP下采用DirectShow采集摄像头.doc_第3页
第3页 / 共20页
XP下采用DirectShow采集摄像头.doc_第4页
第4页 / 共20页
XP下采用DirectShow采集摄像头.doc_第5页
第5页 / 共20页
点击查看更多>>
资源描述

1、XP 下采用DirectShow 采集摄像头 分类: TCPMP/DDShow/音视频编解码技术 2012-05-24 16:426602 人阅读评论(14)收藏举报xpnullgraphfilterinterfacefunction目录 (?)+转载请标明是引用于 http:/ 欢迎大家提出意见,一起讨论!需要示例源码的请独自联系我.前提: 摄像头能正常工作、摄像头有创建 directshow filter即 大家可以对比我的另一篇文章学习: wince系统下 DirectShow 采集摄像头一、初始化工作1、DirctShow 环境初始化cppview plaincopyprint?1.

2、bool 2. uEye_DirectShow_Demo_Dlg:DirectShow_Init() 3. 4. / initialize the COM library on the current thread 5. HRESULT err= CoInitialize(NULL); 6. 7. if( FAILED(err) 8. 9. MessageBoxEx( NULL, “Initializing COM library failed!“, _FUNCTION_, MB_ICONERROR, 0); 10. 11. 12. return err = S_OK; 13. 2、搜索 Vi

3、deo 源如果没有设备接入,那么 CreateClassEnumerator 会返回失败cppview plaincopyprint?1. bool 2. uEye_DirectShow_Demo_Dlg:VideoSourcesList_Fill() 3. 4. HRESULT status= S_OK; 5. 6. / create System Device Enumerator 7. ICreateDevEnum *pSystemDeviceEnumerator= NULL; 8. status= CoCreateInstance( CLSID_SystemDeviceEnum, 9.

4、 NULL, 10. CLSCTX_INPROC, 11. IID_ICreateDevEnum, 12. (void*) 13. if( FAILED(status) 14. 15. MessageBoxEx( NULL, “Creating System Device Enumerator failed!“, _FUNCTION_, MB_ICONERROR, 0); 16. return false; 17. 18. 19. / create Class Enumerator that lists alls video input devices among the system dev

5、ices 20. IEnumMoniker *pVideoInputDeviceEnumerator= NULL; 21. status= pSystemDeviceEnumerator-CreateClassEnumerator( CLSID_VideoInputDeviceCategory, 22. 24. 25. / release the System Device Enumerator which is not needed anymore 26. pSystemDeviceEnumerator-Release(); 27. pSystemDeviceEnumerator= NULL

6、; 28. 29. if( status != S_OK) 30. 31. MessageBoxEx( NULL, “Creating Class Enumerator failed!“, _FUNCTION_, MB_ICONERROR, 0); 32. return false; 33. 34. 35. / add entry no device selected to list 36. m_comboVideoSources.AddString( “no device selected“); 37. m_comboVideoSources.SetItemDataPtr( 0, NULL)

7、; 38. 39. / for each enumerated video input device: add it to the list 40. IMoniker *pMoniker= NULL; 41. while( pVideoInputDeviceEnumerator-Next( 1, 44. VariantInit( 45. 46. / make filters properties accessible 47. IPropertyBag *pPropBag= NULL; 48. status= pMoniker-BindToStorage( 0, 0, IID_IProperty

8、Bag, (void*) 49. if( FAILED(status) 50. 51. pPropBag= NULL; 52. MessageBoxEx( NULL, “Accessing filter properties failed!“, _FUNCTION_, MB_ICONERROR, 0); 53. / continue with the next filter 54. 55. else 56. 57. / add a reference to the storage object 58. pPropBag-AddRef(); 59. 60. / get the name of t

9、his filter 61. status= pPropBag-Read( L“FriendlyName“, 62. if( FAILED(status) 63. 64. MessageBoxEx( NULL, “Reading filter name failed!“, _FUNCTION_, MB_ICONERROR, 0); 65. / continue with the next filter 66. 67. else 68. 69. / if uEye Capture Device: 70. / add filtername to the list and link the moni

10、ker pointer to the list entry 71. CString sTemp(var.bstrVal); 72. #if (0) /* jma 04/08/2010 add devices named UI. too */ 73. if( sTemp.Find( “uEye Capture Device“, 0) != -1) 74. #endif 75. if (sTemp.Find( “uEye Capture Device“, 0) != -1) | (sTemp.Find( “UI“, 0) != -1) 76. 77. int index = m_comboVide

11、oSources.AddString( sTemp); 78. / dont forget to release the moniker later! 79. m_comboVideoSources.SetItemDataPtr( index, pMoniker); 80. 81. else 82. 83. pMoniker-Release(); 84. pMoniker= NULL; 85. 86. 87. 88. / release the reference to the storage object 89. pPropBag-Release(); 90. pPropBag= NULL;

12、 91. 92. 93. VariantClear( 94. 95. 96. / release the class enumerator 97. pVideoInputDeviceEnumerator-Release(); 98. pVideoInputDeviceEnumerator= NULL; 99. 100. / select first list entry 101. m_comboVideoSources.SetCurSel( 0); 102. 103. return false; 104. 二、选择某个摄像设备1、先停止原有的 Mediacppview plaincopypri

13、nt?1. bool 2. uEye_DirectShow_Demo_Dlg:FilterGraph_Stop() 3. 4. Invalidate(); 5. 6. / proceed only if filter graph manager object present 7. if( m_pActiveFilterGraphManager = NULL) 8. 9. return true; 10. 11. 12. HRESULT status= S_OK; 13. 14. / get the MediaControl interface of the graph 15. IMediaCo

14、ntrol* pMediaControl= NULL; 16. status= m_pActiveFilterGraphManager-QueryInterface( IID_IMediaControl, (void*) 17. if( FAILED(status) | pMediaControl = NULL) 18. 19. MessageBoxEx( NULL, “Querying MediaControl interface of the filter graph manager failed!“, _FUNCTION_, MB_ICONERROR, 0); 20. return fa

15、lse; 21. 22. 23. / check for graph to be executing before allowing to stop 24. 25. /OAFilterState filterState= 0; / OAFilterState is actually a long 26. /status= pMediaControl-GetState( 100, 27. /if( FAILED(status) 28. / 29. / / cleanup 30. 31. / / release the MediaControl interface object 32. / pMe

16、diaControl-Release(); 33. / pMediaControl= NULL; 34. 35. / MessageBoxEx( NULL, “Querying graph state failed!“, _FUNCTION_, MB_ICONERROR, 0); 36. / return false; 37. / 38. 39. /if( filterState != State_Stopped) 40. 41. / stop the execution of the graph 42. status= pMediaControl-Stop(); 43. if( FAILED

17、(status) 44. 45. / cleanup 46. 47. / release the MediaControl interface object 48. pMediaControl-Release(); 49. pMediaControl= NULL; 50. 51. MessageBoxEx( NULL, “Stopping the graph failed!“, _FUNCTION_, MB_ICONERROR, 0); 52. return false; 53. 54. 55. / update the graph state view 56. UpdateGraphStat

18、e( State_Stopped); 57. 58. 59. / release the MediaControl interface object 60. pMediaControl-Release(); 61. pMediaControl= NULL; 62. 63. return true; 64. 2、删除 Graphcppview plaincopyprint?1. bool 2. uEye_DirectShow_Demo_Dlg:FilterGraph_Destroy() 3. 4. / proceed only if filter graph manager object pre

19、sent 5. if( m_pActiveFilterGraphManager = NULL) 6. 7. return true; 8. 9. 10. if( !FilterGraph_Stop() 11. 12. return false; 13. 14. 15. / disable dump graph button as long as no graph present 16. m_bnDumpGraph.EnableWindow( FALSE); 17. 18. / delete the capture filter 19. m_pActiveVideoSource-Release(

20、); 20. m_pActiveVideoSource= NULL; 21. 22. / delete the graph 23. m_pActiveFilterGraphManager-Release(); 24. m_pActiveFilterGraphManager= NULL; 25. 26. / update the graph state view 27. UpdateGraphState( -1); 28. 29. return true; 30. 3、根据选择的设备的 moniker来创建 Graph分为:(1)为选择的设备创建 capture filterstatus= pM

21、oniker-BindToObject( 0, 0, IID_IBaseFilter, (void*)(2) 创建一个 capture Graph Builder 对象ICaptureGraphBuilder2* pCaptureGraphBuilder= NULL;status= CoCreateInstance( CLSID_CaptureGraphBuilder2,NULL,CLSCTX_INPROC,IID_ICaptureGraphBuilder2,(void*)(3) 创建 Filter Graph Manager/ create the Filter Graph Managers

22、tatus= CoCreateInstance( CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void *)(4) 初始化 Capture Graph Builder 对象,把 graph 和 builder 连接起来status= pCaptureGraphBuilder-SetFiltergraph( m_pActiveFilterGraphManager);(5) 增加 Capture 到graph中status= m_pActiveFilterGraphManager-AddFilter( m_pActiveVideo

23、Source, L“Video Capture“);(6) render 视频的 capture pin, 把 caputre filter 和 render 连接起来status= pCaptureGraphBuilder-RenderStream( (7) 查询 filter graph manager 的 VideoWindow 接口IVideoWindow* pVideoWindow= NULL;status= m_pActiveFilterGraphManager-QueryInterface( IID_IVideoWindow, (void*)(8) 把 video view 连接

24、到 graph/ connect the dialogs video view to the graphCWnd* pwnd= GetDlgItem( IDC_STATIC_VIDEOVIEW);pVideoWindow-put_Owner( (OAHWND)pwnd-m_hWnd); / put_Owner always returns NOERROR/ we dont want a title bar on our video viewlong pWindowStyle = 0;pVideoWindow-get_WindowStyle(pWindowStyle pVideoWindow-p

25、ut_WindowStyle(pWindowStyle);/ adjust graphs video geometryCRect rc( 0, 0, 0, 0);pwnd-GetClientRect( pVideoWindow-SetWindowPosition( rc.left, rc.top, rc.Width(), rc.Height();/ release the VideoWindow interface object, we do not need it anymorepVideoWindow-Release();四、 开始播放cppview plaincopyprint?1. b

26、ool 2. uEye_DirectShow_Demo_Dlg:FilterGraph_Start() 3. 4. / proceed only if filter graph manager object present 5. if( m_pActiveFilterGraphManager = NULL) 6. 7. return true; 8. 9. 10. HRESULT status= S_OK; 11. 12. / get the MediaControl interface of the graph 13. IMediaControl* pMediaControl= NULL;

27、14. status= m_pActiveFilterGraphManager-QueryInterface( IID_IMediaControl, (void*) 15. if( FAILED(status) | pMediaControl = NULL) 16. 17. MessageBoxEx( NULL, “Querying MediaControl interface of the filter graph manager failed!“, _FUNCTION_, MB_ICONERROR, 0); 18. return false; 19. 20. 21. / check for

28、 graph to be stopped before allowing to start 22. 23. /OAFilterState filterState= 0; / OAFilterState is actually a long 24. /status= pMediaControl-GetState( 100, 25. /if( FAILED(status) 26. / 27. / / cleanup 28. 29. / / release the MediaControl interface object 30. / pMediaControl-Release(); 31. / p

29、MediaControl= NULL; 32. 33. / MessageBoxEx( NULL, “Querying graph state failed!“, _FUNCTION_, MB_ICONERROR, 0); 34. / return false; 35. / 36. 37. /if( filterState = State_Stopped) 38. 39. / start the execution of the graph 40. status= pMediaControl-Run(); 41. if( FAILED(status) 42. 43. / cleanup 44.

30、 45. / release the MediaControl interface object 46. pMediaControl-Release(); 47. pMediaControl= NULL; 48. 49. MessageBoxEx( NULL, “Starting the graph failed!“, _FUNCTION_, MB_ICONERROR, 0); 50. return false; 51. 52. 53. / update the graph state view 54. UpdateGraphState( State_Running); 55. 56. 57.

31、 / release the MediaControl interface object 58. pMediaControl-Release(); 59. pMediaControl= NULL; 60. 61. return true; 62. 五、 pin 特征的查看如果 pin里有自己的查看特征接口, 那么就直接调用接口cppview plaincopyprint?1. bool 2. uEye_DirectShow_Demo_Dlg:ShowPinProperties() 3. 4. / proceed only if video source object present 5. if

32、( m_pActiveVideoSource = NULL) 6. 7. return true; 8. 9. 10. HRESULT status= S_OK; 11. 12. IPin* pPin= GetPin( m_pActiveVideoSource, PINDIR_OUTPUT, 13. if( pPin = NULL) 14. 15. MessageBoxEx( NULL, “Pin not available!“, _FUNCTION_, MB_ICONERROR, 0); 16. return false; 17. 18. 19. IUnknown* pFilterUnk=

33、NULL; 20. status= pPin-QueryInterface( IID_IUnknown, (void*) 21. if( FAILED(status) 22. 23. / cleanup 24. 25. pPin-Release(); 26. pPin= NULL; 27. 28. MessageBoxEx( NULL, “Querying pins IAMStreamConfig interface failed!“, _FUNCTION_, MB_ICONERROR, 0); 29. return false; 30. 31. 32. ISpecifyPropertyPag

34、es* pSpecifyPropertyPages= NULL; 33. status= pFilterUnk-QueryInterface( IID_ISpecifyPropertyPages, (void*) 34. if( FAILED(status) 35. 36. / cleanup 37. 38. pFilterUnk-Release(); 39. pFilterUnk= NULL; 40. 41. pPin-Release(); 42. pPin= NULL; 43. 44. MessageBoxEx( NULL, “Querying pins ISpecifyPropertyP

35、ages interface failed!“, _FUNCTION_, MB_ICONERROR, 0); 45. return false; 46. 47. 48. CAUUID cauuid= 0, NULL; 49. status= pSpecifyPropertyPages-GetPages( 50. if( FAILED(status) 51. 52. / cleanup 53. 54. pSpecifyPropertyPages-Release(); 55. pSpecifyPropertyPages-Release(); 56. pSpecifyPropertyPages= N

36、ULL; 57. 58. pFilterUnk-Release(); 59. pFilterUnk= NULL; 60. 61. pPin-Release(); 62. pPin= NULL; 63. 64. MessageBoxEx( NULL, “Querying pins ISpecifyPropertyPages interface failed!“, _FUNCTION_, MB_ICONERROR, 0); 65. return false; 66. 67. pSpecifyPropertyPages-Release(); 68. pSpecifyPropertyPages-Rel

37、ease(); 69. pSpecifyPropertyPages= NULL; 70. 71. status= OleCreatePropertyFrame( GetSafeHwnd(),/*this, 72. 0, 73. 0, 74. OLESTR(“uEye Capture Device Pin“), 75. 1, 76. (IUnknown*) 82. 83. if( FAILED(status) 84. 85. / cleanup 86. 87. CoTaskMemFree( cauuid.pElems); 88. cauuid.pElems= NULL; 89. cauuid.c

38、Elems= 0; 90. 91. pFilterUnk-Release(); 92. pFilterUnk= NULL; 93. 94. pPin-Release(); 95. pPin= NULL; 96. 97. MessageBoxEx( NULL, “OleCreatePropertyFrame failed!“, _FUNCTION_, MB_ICONERROR, 0); 98. return false; 99. 100. 101. / cleanup 102. 103. CoTaskMemFree( cauuid.pElems); 104. cauuid.pElems= NUL

39、L; 105. cauuid.cElems= 0; 106. 107. pFilterUnk-Release(); 108. pFilterUnk= NULL; 109. 110. pPin-Release(); 111. pPin= NULL; 112. 113. return true; 114. 六、 查询包含的 filter 信息cppview plaincopyprint?1. bool 2. uEye_DirectShow_Demo_Dlg:EnumerateFilters() 3. 4. / proceed only if filter graph builder object

40、present 5. if( m_pActiveFilterGraphManager = NULL) 6. 7. return false; 8. 9. 10. IEnumFilters *pEnum = NULL; 11. IBaseFilter *pFilter; 12. ULONG cFetched; 13. 14. HRESULT hr = m_pActiveFilterGraphManager-EnumFilters( 15. if (FAILED(hr) 16. 17. MessageBoxEx( NULL, “Enumerating filters failed!“, _FUNC

41、TION_, MB_ICONERROR, 0); 18. return false; 19. 20. 21. CString sInfo( “Filters in the Graph:nn“); 22. int i= 0; 23. 24. while(pEnum-Next(1, 27. 28. FILTER_INFO FilterInfo; 29. hr = pFilter-QueryFilterInfo( 30. if (FAILED(hr) 31. 32. MessageBoxEx( NULL, “Could not get the filter info“, _FUNCTION_, MB

42、_ICONERROR, 0); 33. continue; / Maybe the next one will work. 34. 35. 36. LPWSTR pVendorInfo= NULL; 37. hr= pFilter-QueryVendorInfo( 38. if( FAILED(hr) 39. 40. pVendorInfo= NULL; 41. 42. 43. 44. if( pVendorInfo != NULL) 45. 46. sInfo.AppendFormat( “%d: %S (by %S)n“, i, FilterInfo.achName, pVendorInf

43、o); 47. CoTaskMemFree( pVendorInfo); 48. pVendorInfo= NULL; 49. 50. else 51. 52. sInfo.AppendFormat( “%d: %Sn“, i, FilterInfo.achName); 53. 54. 55. / The FILTER_INFO structure holds a pointer to the Filter Graph 56. / Manager, with a reference count that must be released. 57. if (FilterInfo.pGraph != NULL) 58. 59. FilterInfo.pGraph-Release(); 60. 61. pFilter-Release(); 62. 63. 64. pEnum-Release(); 65. 66. MessageBoxEx( NULL, sInfo.GetBuffer(), _FUNCTION_, MB_OK, 0); 67. 68. return true; 69.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报