Windows平台使用DirectShow获取UVC摄像头上按键
2018-10-18 本文已影响0人
雷动软件
什么是UVC摄像头,UVC是什么意思?
UVC是一种标准的USB视频设备协议,就是传说中的免驱摄像头。UVC是Usb Video Class的简称。
Windows平台上UVC驱动文档
DirectShow简介
DirectShow是微软公司在ActiveMovie和Video for Windows的基础上推出的新一代基于COM(Component Object Model)的流媒体处理的开发包,与DirectX开发包一起发布。DirectShow使用一种叫Filter Graph的模型来管理整个数据流的处理过程,运用DirectShow,我们可以很方便地从支持WDM驱动模型的采集卡上捕获数据,并且进行相应的后期处理乃至存储到文件中。这样使在多媒体数据库管理系统(MDBMS)中多媒体数据的存取变得更加方便。它广泛地支持各种媒体格式,包括Asf、Mpeg、Avi、Dv、Mp3、Wave等,为多媒体流的捕捉和回放提供了强有力的支持。
调用DirectShow获取摄像头按键后的抓拍图片
Filter
DirectShow使用模块化体系结构,其中每个处理阶段都由称为filter的COM对象完成。 DirectShow为应用程序提供了一组标准filter,开发人员可以编写自己的自定义过滤器来扩展DirectShow的功能。
Graph
有多个Filter连接,构成Graph。Filter Graph Manager用来构建和控制Graph中的Filter
代码实战
初始化各个Com接口
HRESULT GetInterfaces(void)
{
HRESULT hr;
//创建Filter Graph Manager.
hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IGraphBuilder, (void **) &pGraphBuilder);
if (FAILED(hr))
return hr;
//创建Capture Graph Builder.
hr = CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
IID_ICaptureGraphBuilder2, (void **) &pCaptureGraphBuilder2);
if (FAILED(hr))
return hr;
// IMediaControl接口,用来控制流媒体在Filter Graph中的流动,例如流媒体的启动和停止;
hr = pGraphBuilder->QueryInterface(IID_IMediaControl,(LPVOID *) &pMediaControl);
if (FAILED(hr))
return hr;
// IVideoWindow,用来显示预览视频
hr = pGraphBuilder->QueryInterface(IID_IVideoWindow, (LPVOID *) &pVideoWindow);
if (FAILED(hr))
return hr;
// IMediaEvent接口,该接口在Filter Graph发生一些事件时用来创建事件的标志信息并传送给应用程序
hr = pGraphBuilder->QueryInterface(IID_IMediaEvent,(LPVOID *) &pMediaEvent);
if (FAILED(hr))
return hr;
//创建用于预览的Sample Grabber Filter.
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&pGrabberF);
if (FAILED(hr))
return hr;
//获取ISampleGrabber接口,用于设置回调等相关信息
hr = pGrabberF->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabber);
if (FAILED(hr))
return hr;
//创建用于抓拍的Sample Grabber Filter.
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,IID_IBaseFilter, (void**)&pGrabberStill);
if (FAILED(hr))
return hr;
//获取ISampleGrabber接口,用于设置回调等相关信息
hr = pGrabberStill->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabberStill);
if (FAILED(hr))
return hr;
//创建Null Filter
hr = CoCreateInstance(CLSID_NullRenderer,NULL,CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void**)&pNull);
if (FAILED(hr))
return hr;
return hr;
}
检测设备
//遍历设备,找到指定vidpid设备后打开设备
HRESULT InitMonikers()
{
USES_CONVERSION;
HRESULT hr;
ULONG cFetched;
//https://docs.microsoft.com/zh-cn/windows/desktop/DirectShow/selecting-a-capture-device
//遍历设备
ICreateDevEnum *pCreateDevEnum;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum);
if (FAILED(hr))
{
printf("Failed to enumerate all video and audio capture devices! hr=0x%x\n", hr);
return hr;
}
IEnumMoniker *pEnumMoniker;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumMoniker, 0);
if (FAILED(hr) || !pEnumMoniker)
{
printf("Failed to create ClassEnumerator! hr=0x%x\n", hr);
return -1;
}
while (hr = pEnumMoniker->Next(1, &pMonikerVideo, &cFetched), hr == S_OK)
{
IPropertyBag *pPropBag;
//BindToStorage之后就可以访问设备标识的属性集了。
HRESULT hr = pMonikerVideo->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
if (FAILED(hr))
{
pMonikerVideo->Release();
continue;
}
VARIANT var;
VariantInit(&var);
//DevicePath中包含vidpid
hr = pPropBag->Read(L"DevicePath", &var, 0);
if (FAILED(hr))
{
VariantClear(&var);
pMonikerVideo->Release();
continue;
}
//比较是否是要使用的设备
//TRACE("Device path: %S\n", var.bstrVal);
std::string devpath = std::string(W2A(var.bstrVal));
if (devpath.find("vid_06f8&pid_3015") == -1)
{
VariantClear(&var);
pMonikerVideo->Release();
continue;
}
VariantClear(&var);
// BindToObject将某个设备标识绑定到一个DirectShow Filter,
// 然后调用IFilterGraph::AddFilter加入到Filter Graph中,这个设备就可以参与工作了
// 调用IMoniker::BindToObject建立一个和选择的device联合的filter,
// 并且装载filter的属性(CLSID,FriendlyName, and DevicePath)。
hr = pMonikerVideo->BindToObject(0, 0, IID_IBaseFilter, (void**)&pVideoCaptureFilter);
if (FAILED(hr))
{
printf("Couldn't bind moniker to filter object! hr=0x%x\n", hr);
return hr;
}
pPropBag->Release();
pMonikerVideo->Release();
}
pEnumMoniker->Release();
return hr;
}
构建预览Graph
//开始构建预览graph
//加入设备抓拍filter
hr = pGraphBuilder->AddFilter(pVideoCaptureFilter, L"Video Capture");
if (FAILED(hr))
{
printf("Couldn't add video capture filter to graph! hr=0x%x\n", hr);
pVideoCaptureFilter->Release();
return hr;
}
//加入DirectShow中自带的SampleGrabber Filter
hr = pGraphBuilder->AddFilter(pGrabberF, L"Sample Grabber");
if (FAILED(hr))
{
printf("Couldn't add sample grabber to graph! hr=0x%x\n", hr);
// Return an error.
}
//使用Capture Graph Builder构建预览 Graph
hr = pCaptureGraphBuilder2->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pVideoCaptureFilter, pGrabberF, 0 );
if (FAILED(hr))
{
printf("Couldn't render video capture stream. The device may already be in use. hr=0x%x\n", hr);
pVideoCaptureFilter->Release();
return hr;
}
构建抓拍Graph
//加入DirectShow中自带的SampleGrabber Filter
hr = pGraphBuilder->AddFilter(pGrabberStill, L"Still Sample Grabber");
if (FAILED(hr))
{
printf("Couldn't add sample grabber to graph! hr=0x%x\n", hr);
// Return an error.
}
//加入DirectShow中自带的NullRender Filter
hr = pGraphBuilder->AddFilter(pNull, L"NullRender");
if (FAILED(hr))
{
printf("Couldn't add null to graph! hr=0x%x\n", hr);
return hr;
}
//使用Capture Graph Builder构建抓拍 Graph
hr = pCaptureGraphBuilder2->RenderStream(&PIN_CATEGORY_STILL, &MEDIATYPE_Video, pVideoCaptureFilter, pGrabberStill, pNull);
if (FAILED(hr))
{
printf("Couldn't render video capture stream. The device may already be in use. hr=0x%x\n", hr);
pVideoCaptureFilter->Release();
return hr;
}
//configure the Sample Grabber so that it buffers samples :
hr = pSampleGrabberStill->SetOneShot(FALSE);
hr = pSampleGrabberStill->SetBufferSamples(TRUE);
//获取设备输出格式信息
AM_MEDIA_TYPE mt;
hr = pSampleGrabber->GetConnectedMediaType(&mt);
if (FAILED(hr))
{
return -1;
}
VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*)mt.pbFormat;
CSampleGrabberCB *CB = new CSampleGrabberCB();
if (!FAILED(hr))
{
CB->Width = vih->bmiHeader.biWidth;
CB->Height = vih->bmiHeader.biHeight;
}
//设置触发抓拍后,调用的回调函数 0-调用SampleCB 1-BufferCB
hr = pSampleGrabberStill->SetCallback(CB, 1);
if (FAILED(hr))
{
printf("set still trigger call back failed\n");
}
启动Graph
//设置预览窗口大小位置
SetupVideoWindow();
//启动设备
hr = pMediaControl->Run();
if (FAILED(hr))
{
printf("Couldn't run the graph! hr=0x%x\n", hr);
return hr;
}
else
psCurrent = Running;
使用GraphEdt进行调试
GraphEdt.exe是windows sdk中的graph调试工具,使用方法简介如下:
-
查看设备
在Graph菜单中点击Insert Filters,找到Video Capture Sources
微信图片_20181018093403.png
可以看到设备信息,点击Insert Filter可以将设备Filter加入到Graph中。
- 构建Graph
如果不清楚设备FIlter后边应该假如什么Filter,可以在Filter的Pin上右键,选择Render Filter,会自动连接一个Video Render。
微信图片_20181018093830.png
此时点击播放按钮,可以看到视频