转载请标明是引用于 http://blog.csdn.net/chenyujing1234

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

须要演示样例源代码的请独自联系我.

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

大家能够对照我的还有一篇文章学习:    wince系统下DirectShow採集摄像头

一、初始化工作

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

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

bool
uEye_DirectShow_Demo_Dlg::VideoSourcesList_Fill()
{
HRESULT status= S_OK; // create System Device Enumerator
ICreateDevEnum *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 devices
IEnumMoniker *pVideoInputDeviceEnumerator= NULL;
status= pSystemDeviceEnumerator->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
&pVideoInputDeviceEnumerator,
0); // release the System Device Enumerator which is not needed anymore
pSystemDeviceEnumerator->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 list
m_comboVideoSources.AddString( "[no device selected]");
m_comboVideoSources.SetItemDataPtr( 0, NULL); // for each enumerated video input device: add it to the list
IMoniker *pMoniker= NULL;
while( pVideoInputDeviceEnumerator->Next( 1, &pMoniker, NULL) == S_OK )
{
VARIANT var;
VariantInit(&var); // make filters properties accessible
IPropertyBag *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 object
pPropBag->AddRef(); // get the name of this filter
status= 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 entry
CString sTemp(var.bstrVal);
#if (0) /* jma [04/08/2010] add devices named UI... too */
if( sTemp.Find( "uEye Capture Device", 0) != -1)
#endif
if ((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 object
pPropBag->Release();
pPropBag= NULL;
} VariantClear(&var);
} // release the class enumerator
pVideoInputDeviceEnumerator->Release();
pVideoInputDeviceEnumerator= NULL; // select first list entry
m_comboVideoSources.SetCurSel( 0); return false;
}

二、选择某个摄像设备

1、先停止原有的Media
bool
uEye_DirectShow_Demo_Dlg::FilterGraph_Stop()
{
Invalidate(); // proceed only if filter graph manager object present
if( m_pActiveFilterGraphManager == NULL)
{
return true;
} HRESULT status= S_OK; // get the MediaControl interface of the graph
IMediaControl* 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 graph
status= pMediaControl->Stop();
if( FAILED(status))
{
// cleanup // release the MediaControl interface object
pMediaControl->Release();
pMediaControl= NULL; MessageBoxEx( NULL, "Stopping the graph failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} // update the graph state view
UpdateGraphState( State_Stopped);
} // release the MediaControl interface object
pMediaControl->Release();
pMediaControl= NULL; return true;
}
2、删除Graph
bool
uEye_DirectShow_Demo_Dlg::FilterGraph_Destroy()
{
// proceed only if filter graph manager object present
if( m_pActiveFilterGraphManager == NULL)
{
return true;
} if( !FilterGraph_Stop())
{
return false;
} // disable 'dump graph' button as long as no graph present
m_bnDumpGraph.EnableWindow( FALSE); // delete the capture filter
m_pActiveVideoSource->Release();
m_pActiveVideoSource= NULL; // delete the graph
m_pActiveFilterGraphManager->Release();
m_pActiveFilterGraphManager= NULL; // update the graph state view
UpdateGraphState( -1); return true;
}
3、依据选择的设备的moniker来创建Graph

分为:

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

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

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

ICaptureGraphBuilder2* pCaptureGraphBuilder= NULL;

    status= CoCreateInstance(   CLSID_CaptureGraphBuilder2,

                                NULL,

                                CLSCTX_INPROC,

                                IID_ICaptureGraphBuilder2,

                                (void**)&pCaptureGraphBuilder);

(3) 创建 Filter Graph Manager

// create the Filter Graph Manager

    status= CoCreateInstance(   CLSID_FilterGraph,

                                NULL,

                                CLSCTX_INPROC,

                                IID_IGraphBuilder,

                                (void **)&m_pActiveFilterGraphManager);

(4) 初始化Capture Graph Builder 对象,把graph和builder 连接起来

status= pCaptureGraphBuilder->SetFiltergraph( m_pActiveFilterGraphManager);

(5) 添加Capture到graph中

status= m_pActiveFilterGraphManager->AddFilter( m_pActiveVideoSource, L"Video Capture");

(6) render 视频的capture pin, 把caputre filter和render连接起来

status= pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE,

                                                &MEDIATYPE_Video,

                                                m_pActiveVideoSource,

                                                NULL,

                                                NULL);

(7) 查询filter graph manager的VideoWindow接口

IVideoWindow* pVideoWindow= NULL;

    status= m_pActiveFilterGraphManager->QueryInterface( IID_IVideoWindow, (void**)&pVideoWindow);

(8) 把video view连接到graph

// connect the dialogs video view to the graph

    CWnd* 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 view

    long pWindowStyle = 0;

    pVideoWindow->get_WindowStyle(&pWindowStyle);

    pWindowStyle &= ~WS_CAPTION;

    pVideoWindow->put_WindowStyle(pWindowStyle);

// adjust graphs video geometry

    CRect rc( 0, 0, 0, 0);

    pwnd->GetClientRect( &rc);

    pVideoWindow->SetWindowPosition( rc.left, rc.top, rc.Width(), rc.Height());

// release the VideoWindow interface object, we do not need it anymore

    pVideoWindow->Release();

四、  開始播放

bool
uEye_DirectShow_Demo_Dlg::FilterGraph_Start()
{
// proceed only if filter graph manager object present
if( m_pActiveFilterGraphManager == NULL)
{
return true;
} HRESULT status= S_OK; // get the MediaControl interface of the graph
IMediaControl* 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 stopped before allowing to start //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)
{
// start the execution of the graph
status= pMediaControl->Run();
if( FAILED(status))
{
// cleanup // release the MediaControl interface object
pMediaControl->Release();
pMediaControl= NULL; MessageBoxEx( NULL, "Starting the graph failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} // update the graph state view
UpdateGraphState( State_Running);
} // release the MediaControl interface object
pMediaControl->Release();
pMediaControl= NULL; return true;
}

五、 pin 特征的查看

假设pin里有自己的查看特征接口, 那么就直接调用接口

bool
uEye_DirectShow_Demo_Dlg::ShowPinProperties()
{
// proceed only if video source object present
if( m_pActiveVideoSource == NULL)
{
return true;
} HRESULT status= S_OK; IPin* pPin= GetPin( m_pActiveVideoSource, PINDIR_OUTPUT, &MEDIATYPE_Video, &PIN_CATEGORY_CAPTURE);
if( pPin == NULL)
{
MessageBoxEx( NULL, "Pin not available!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} IUnknown* pFilterUnk= NULL;
status= pPin->QueryInterface( IID_IUnknown, (void**)&pFilterUnk);
if( FAILED(status))
{
// cleanup pPin->Release();
pPin= NULL; MessageBoxEx( NULL, "Querying pin's IAMStreamConfig interface failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} ISpecifyPropertyPages* pSpecifyPropertyPages= NULL;
status= pFilterUnk->QueryInterface( IID_ISpecifyPropertyPages, (void**)&pSpecifyPropertyPages);
if( FAILED(status))
{
// cleanup pFilterUnk->Release();
pFilterUnk= NULL; pPin->Release();
pPin= NULL; MessageBoxEx( NULL, "Querying pin's ISpecifyPropertyPages interface failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} CAUUID cauuid= { 0, NULL};
status= pSpecifyPropertyPages->GetPages( &cauuid);
if( FAILED(status))
{
// cleanup pSpecifyPropertyPages->Release();
pSpecifyPropertyPages->Release();
pSpecifyPropertyPages= NULL; pFilterUnk->Release();
pFilterUnk= NULL; pPin->Release();
pPin= NULL; MessageBoxEx( NULL, "Querying pin's ISpecifyPropertyPages interface failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
}
pSpecifyPropertyPages->Release();
pSpecifyPropertyPages->Release();
pSpecifyPropertyPages= NULL; status= OleCreatePropertyFrame( GetSafeHwnd(),//*this,
0,
0,
OLESTR("uEye Capture Device Pin"),
1,
(IUnknown**)&pFilterUnk,
cauuid.cElems,
(GUID*)cauuid.pElems,
0,
0,
NULL); if( FAILED(status))
{
// cleanup CoTaskMemFree( cauuid.pElems);
cauuid.pElems= NULL;
cauuid.cElems= 0; pFilterUnk->Release();
pFilterUnk= NULL; pPin->Release();
pPin= NULL; MessageBoxEx( NULL, "OleCreatePropertyFrame failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} // cleanup CoTaskMemFree( cauuid.pElems);
cauuid.pElems= NULL;
cauuid.cElems= 0; pFilterUnk->Release();
pFilterUnk= NULL; pPin->Release();
pPin= NULL; return true;
}

六、 查询包括的 filter信息

bool
uEye_DirectShow_Demo_Dlg::EnumerateFilters()
{
// proceed only if filter graph builder object present
if( m_pActiveFilterGraphManager == NULL)
{
return false;
} IEnumFilters *pEnum = NULL;
IBaseFilter *pFilter;
ULONG cFetched; HRESULT hr = m_pActiveFilterGraphManager->EnumFilters(&pEnum);
if (FAILED(hr))
{
MessageBoxEx( NULL, "Enumerating filters failed!", __FUNCTION__, MB_ICONERROR, 0);
return false;
} CString sInfo( "Filters in the Graph:\n\n");
int i= 0; while(pEnum->Next(1, &pFilter, &cFetched) == S_OK)
{
i++; FILTER_INFO FilterInfo;
hr = pFilter->QueryFilterInfo(&FilterInfo);
if (FAILED(hr))
{
MessageBoxEx( NULL, "Could not get the filter info", __FUNCTION__, MB_ICONERROR, 0);
continue; // Maybe the next one will work.
} LPWSTR pVendorInfo= NULL;
hr= pFilter->QueryVendorInfo( &pVendorInfo);
if( FAILED(hr))
{
pVendorInfo= NULL;
} if( pVendorInfo != NULL)
{
sInfo.AppendFormat( "%d: %S (by %S)\n", i, FilterInfo.achName, pVendorInfo);
CoTaskMemFree( pVendorInfo);
pVendorInfo= NULL;
}
else
{
sInfo.AppendFormat( "%d: %S\n", i, FilterInfo.achName);
} // The FILTER_INFO structure holds a pointer to the Filter Graph
// Manager, with a reference count that must be released.
if (FilterInfo.pGraph != NULL)
{
FilterInfo.pGraph->Release();
}
pFilter->Release();
} pEnum->Release(); MessageBoxEx( NULL, sInfo.GetBuffer(), __FUNCTION__, MB_OK, 0); return true;
}

XP下採用DirectShow採集摄像头的更多相关文章

  1. Windows XP下安装WinCE6.0开发环境

    Windows下怎样编译WinCE6.0及开发应用程序.以下介绍(安装之前必须保证C盘有足够的空间!20g左右!主要是由于在安装程序在安装过程中要解压): 在Visual Studio 2005之前, ...

  2. CentOS下Storm 1.0.0集群安装具体解释

    本文环境例如以下: 操作系统:CentOS 6 32位 ZooKeeper版本号:3.4.8 Storm版本号:1.0.0 JDK版本号:1.8.0_77 32位 python版本号:2.6.6 集群 ...

  3. cefSharp在XP下使得程序崩溃记录

    前言:这是一个奇葩的问题,到现在自己还没有搞明白问题出现在哪里,但是从问题总算是解决了,希望看到此文章的大牛,如果知道问题出在什么地方,可以告知一下. [一个在XP系统下面应用程序崩溃问题] 资源: ...

  4. (转载)用VS2012或VS2013在win7下编写的程序在XP下运行就出现“不是有效的win32应用程序“

    原文地址:http://www.vcerror.com/?p=1483 问题描述: 用VC2013编译了一个程序,在Windows 8.Windows 7(64位.32位)下都能正常运行.但在Win ...

  5. 如何让VS2012编写的程序在XP下运行

    Win32主程序需要以下设置 第一步:在工程属性General设置 第二步:在C/C++ Code Generation 设置 第三步:SubSystem 和  Minimum Required Ve ...

  6. 如何让VS2013编写的程序在xp下运行

    总体分c++程序和c#程序 1.c++程序 这个用C++编写的程序可以经过设置后在XP下运行,主要的“平台工具集”里修改就可以. 额外说明:(1)程序必须为Dotnet 4.0及以下版本.(XP只支持 ...

  7. C# 基于Directshow.Net lib库 USB摄像头使用DirectShow.NET获取摄像头视频流

    https://blog.csdn.net/u010118312/article/details/91766787 https://download.csdn.net/download/u010118 ...

  8. VS2012 生成可以在XP下运行的exe文件

    1. 在已安装VS2012条件下,安装update,作者已经安装了update3; 2. 相关设置: 设置"平台工具集":在项目右击-属性-常规-在"平台工具集" ...

  9. xp 下查看进程指令

    xp 下快速查看进程及关联 exe 的指令,刚发现,还没有测试 win7 和 win10 支持不支持. wmic process where creationclassname="win32 ...

随机推荐

  1. 【UVA】12299-RMQ with Shifts(线段树)

    改动的时候因为数据非常小,所以能够直接暴力改动,查询的时候利用线段树即可了. 14337858 option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. [黑马程序员] I/O

    ---------------------- ASP.Net+Android+IO开发..Net培训.期待与您交流! ---------------------- 0. IO流概述: Java对数据的 ...

  3. 熬之滴水穿石:JSP--HTML中的JAVA代码(6)

                                                                       39--JSTL 在JSP编码中需考虑的一种方法,因为这种方法可以 ...

  4. Git 使用规范流程(转)

    团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个人都提交一堆杂乱无章的commit,项目很快就会变得难以协调和维护. 下面是ThoughtBot 的Git使用规范流程.我从中 ...

  5. c++ try throw catch

    c++ try throw catch 这三者联合使用 , try { statement list; } catch( typeA arg ) { statement list; } catch( ...

  6. <摘录>详谈高性能TCP服务器的开发

    对于开发一款高性能服务器程序,广大服务器开发人员在一直为之奋斗和努力.其中一个影响服务器的重要瓶颈就是服务器的网络处理模块.如果一款服务器程序不能及时的处理用户的数据.则服务器的上层业务逻辑再高效也是 ...

  7. Php 解析XML文件

    Php 解析XML文件 Php 解析XML文件,仅供学习參考!演示样例代码例如以下: <?php header("Content-type: text/html; charset=ut ...

  8. HTML中Id和Name的区别

    源地址:http://www.cnblogs.com/laodai/articles/2244215.html 在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号 ...

  9. java.util.zip - Recreating directory structure(转)

    include my own version for your reference. We use this one to zip up photos to download so it works ...

  10. linux下的块设备驱动(二)

    上一章主要讲了请求队列的一系列问题.下面主要说一下请求函数.首先来说一下硬盘类块设备的请求函数. 请求函数可以在没有完成请求队列的中的所有请求的情况下就返回,也可以在一个请求都不完成的情况下就返回. ...