以下为视频采集的主要代码(不全),不可直接编译。
完整代码见http://download.csdn.net/detail/u011073510/8319753
1:枚举设备(有些电脑连接了多个摄像头)
<pre name="code" class="cpp"> //创建COM对象实例
ICreateDevEnum *pCreateDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
(void**)&pCreateDevEnum);
//枚举每一个设备
IMoniker *pM; //用这来表示一个设备
while(hr = pEm->Next(1, &pM, &cFetched), hr == S_OK){} //枚举设备
2:选择一设备(从中选择一个摄像头)
//连接到指定序号的设备
pM->BindToObject(0, 0, IID_IBaseFilter, (void**)pFilter); //pFilter , IBaseFilter**类型 , pM(指定的IMoniker)
//添加IBaseFilter 到 IGraphBuilder
hr = m_pGB->AddFilter(m_pBF, L"Capture Filter");
3:采集视频设置
//创建m_pGrabBase ,由ISampleGrabebr 类型的m_pGrabber 作为参数初始化
CComQIPtr<IBaseFilter, &IID_IBaseFilter> pGrabBase(m_pGrabber);
//添加pGrabBase到IGraphBuilder
hr = m_pGB->AddFilter(pGrabBase, L"Grabber");
//预览或采集视频 开始获取摄像头拍摄的信息
hr = m_pCapture->RenderStream(
&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,m_pBF,pGrabBase,NULL);
//设置回调对象,每次获取完摄像头像素信息后系统自动调用m_cb(CSampleCB类型)的成员函数 BufferCB(double , BYTE *pBuffer, long lBufferSize)
hr = m_pGrabber->SetCallback(&m_cb, 1);