chenyujing1234的专栏

转载请标明是引用于

欢迎大家提出意见,一起讨论!

需要示例源码的请独自联系我.

前提: 摄像头能正常工作、摄像头有创建directshow filter

大家可以对比我的另一篇文章学习: wince系统下DirectShow采集摄像头

一、初始化工作1、DirctShow环境初始化booluEye_DirectShow_Demo_Dlg::DirectShow_Init(){// initialize the COM library on the current threadHRESULT err= CoInitialize(NULL);if( FAILED(err)){MessageBoxEx( NULL, "Initializing COM library failed!", __FUNCTION__, MB_ICONERROR, 0);}return err == S_OK;}2、搜索Video源

如果没有设备接入,那么CreateClassEnumerator会返回失败

booluEye_DirectShow_Demo_Dlg::VideoSourcesList_Fill(){HRESULT status= S_OK;// create System Device EnumeratorICreateDevEnum *pSystemDeviceEnumerator= NULL;status= CoCreateInstance( CLSID_SystemDeviceEnum,NULL,CLSCTX_INPROC,IID_ICreateDevEnum,(void**)&pSystemDeviceEnumerator);if( FAILED(status)){MessageBoxEx( NULL, "Creating System Device Enumerator failed!", __FUNCTION__, MB_ICONERROR, 0);return false;}// create Class Enumerator that lists alls video input devices among the system devicesIEnumMoniker *pVideoInputDeviceEnumerator= NULL;status= pSystemDeviceEnumerator->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,&pVideoInputDeviceEnumerator,0);// release the System Device Enumerator which is not needed anymorepSystemDeviceEnumerator->Release();pSystemDeviceEnumerator= NULL;if( status != S_OK){MessageBoxEx( NULL, "Creating Class Enumerator failed!", __FUNCTION__, MB_ICONERROR, 0);return false;}// add entry ‘[no device selected]’ to listm_comboVideoSources.AddString( "[no device selected]");m_comboVideoSources.SetItemDataPtr( 0, NULL);// for each enumerated video input device: add it to the listIMoniker *pMoniker= NULL;while( pVideoInputDeviceEnumerator->Next( 1, &pMoniker, NULL) == S_OK ){VARIANT var;VariantInit(&var);// make filters properties accessibleIPropertyBag *pPropBag= NULL;status= pMoniker->BindToStorage( 0, 0, IID_IPropertyBag, (void**)&pPropBag);if( FAILED(status)){pPropBag= NULL;MessageBoxEx( NULL, "Accessing filter properties failed!", __FUNCTION__, MB_ICONERROR, 0);// continue with the next filter}else{// add a reference to the storage objectpPropBag->AddRef();// get the name of this filterstatus= pPropBag->Read( L"FriendlyName", &var, 0);if( FAILED(status)){MessageBoxEx( NULL, "Reading filter name failed!", __FUNCTION__, MB_ICONERROR, 0);// continue with the next filter}else{// if uEye Capture Device:// add filtername to the list and link the moniker pointer to the list entryCString sTemp(var.bstrVal);#if (0) /* jma [04/08/2010] add devices named UI… too */if( sTemp.Find( "uEye Capture Device", 0) != -1)#endifif ((sTemp.Find( "uEye Capture Device", 0) != -1) || (sTemp.Find( "UI", 0) != -1)){int index = m_comboVideoSources.AddString( sTemp);// dont forget to release the moniker later!m_comboVideoSources.SetItemDataPtr( index, pMoniker);}else{pMoniker->Release();pMoniker= NULL;}}// release the reference to the storage objectpPropBag->Release();pPropBag= NULL;}VariantClear(&var);}// release the class enumeratorpVideoInputDeviceEnumerator->Release();pVideoInputDeviceEnumerator= NULL;// select first list entrym_comboVideoSources.SetCurSel( 0);return false;}二、选择某个摄像设备1、先停止原有的MediabooluEye_DirectShow_Demo_Dlg::FilterGraph_Stop(){Invalidate();// proceed only if filter graph manager object presentif( m_pActiveFilterGraphManager == NULL){return true;}HRESULT status= S_OK;// get the MediaControl interface of the graphIMediaControl* pMediaControl= NULL;status= m_pActiveFilterGraphManager->QueryInterface( IID_IMediaControl, (void**)&pMediaControl);if( FAILED(status) || pMediaControl == NULL){MessageBoxEx( NULL, "Querying MediaControl interface of the filter graph manager failed!", __FUNCTION__, MB_ICONERROR, 0);return false;}//// check for graph to be executing before allowing to stop//OAFilterState filterState= 0; // OAFilterState is actually a long//status= pMediaControl->GetState( 100, &filterState);//if( FAILED(status))//{// // cleanup// // release the MediaControl interface object// pMediaControl->Release();// pMediaControl= NULL;// MessageBoxEx( NULL, "Querying graph state failed!", __FUNCTION__, MB_ICONERROR, 0);// return false;//}//if( filterState != State_Stopped){// stop the execution of the graphstatus= pMediaControl->Stop();if( FAILED(status)){// cleanup// release the MediaControl interface objectpMediaControl->Release();pMediaControl= NULL;MessageBoxEx( NULL, "Stopping the graph failed!", __FUNCTION__, MB_ICONERROR, 0);return false;}// update the graph state viewUpdateGraphState( State_Stopped);}// release the MediaControl interface objectpMediaControl->Release();pMediaControl= NULL;return true;}2、删除GraphbooluEye_DirectShow_Demo_Dlg::FilterGraph_Destroy(){// proceed only if filter graph manager object presentif( m_pActiveFilterGraphManager == NULL){return true;}if( !FilterGraph_Stop()){return false;}// disable ‘dump graph’ button as long as no graph presentm_bnDumpGraph.EnableWindow( FALSE);// delete the capture filterm_pActiveVideoSource->Release();m_pActiveVideoSource= NULL;// delete the graphm_pActiveFilterGraphManager->Release();m_pActiveFilterGraphManager= NULL;// update the graph state viewUpdateGraphState( -1);return true;}

3、根据选择的设备的moniker来创建Graph

分为:

(1)为选择的设备创建capture filter

status= pMoniker->BindToObject( 0, 0, IID_IBaseFilter, (void**)&m_pActiveVideoSource);

(2) 创建一个capture Graph Builder对象

你要以乐观的态度看待这个世界,你会发现世界是如此得美好

chenyujing1234的专栏

相关文章:

你感兴趣的文章:

标签云: