我在Windows编程方面没有经验,我尝试使用DirectShow从WebCam捕获静态图像。我写了一个基于CommandCam.cpp的小应用程序,可以在这里找到:
http://batchloaf.wordpress.com/commandcam/
我基本上添加了可在此处找到的代码:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd318622%28v=vs.85%29.aspx
但是对pBuilder的调用 - > FindPin(pCap,PINDIR_OUTPUT,& PIN_CATEGORY_STILL,......失败了,我无法获得静止图像的Pin。
我怀疑是因为使用其他网络摄像头程序我可以从我的Microsoft LifeCam Studio获取静态图像。
我想知道我做错了什么?我尝试将调用放在应用程序的不同位置,但它从未成功。
感谢任何提示, 托。
代码的相关部分是这样的: //获取视频输入设备名称 hr = pMoniker-> BindToStorage(0,0,IID_PPV_ARGS(& pPropBag)); VariantInit(安培; VAR); hr = pPropBag-> Read(L“FriendlyName”,& var,0); fprintf(stderr,“捕获设备:%ls \ n”,var.bstrVal); VariantClear(安培; VAR);
// Create capture filter and add to graph
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCap);
if (hr != S_OK) exit_message("Could not create capture filter", 1);
// Add capture filter to graph
hr = pGraph->AddFilter(pCap, L"Capture Filter");
if (hr != S_OK) exit_message("Could not add capture filter to graph", 1);
// Create sample grabber filter
hr = CoCreateInstance(CLSID_SampleGrabber, NULL,
CLSCTX_INPROC_SERVER, IID_IBaseFilter,
(void**)&pSampleGrabberFilter);
if (hr != S_OK)
exit_message("Could not create Sample Grabber filter", 1);
// Query the ISampleGrabber interface of the sample grabber filter
hr = pSampleGrabberFilter->QueryInterface(
DexterLib::IID_ISampleGrabber, (void**)&pSampleGrabber);
if (hr != S_OK)
exit_message("Could not get ISampleGrabber interface to sample grabber filter", 1);
// Enable sample buffering in the sample grabber filter
hr = pSampleGrabber->SetBufferSamples(TRUE);
if (hr != S_OK)
exit_message("Could not enable sample buffering in the sample grabber", 1);
// Set media type in sample grabber filter
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hr = pSampleGrabber->SetMediaType((DexterLib::_AMMediaType *)&mt);
if (hr != S_OK)
exit_message("Could not set media type in sample grabber", 1);
// Add sample grabber filter to filter graph
hr = pGraph->AddFilter(pSampleGrabberFilter, L"SampleGrab");
if (hr != S_OK)
exit_message("Could not add Sample Grabber to filter graph", 1);
// Create Null Renderer filter
hr = CoCreateInstance(CLSID_NullRenderer, NULL,
CLSCTX_INPROC_SERVER, IID_IBaseFilter,
(void**)&pNullRenderer);
if (hr != S_OK)
exit_message("Could not create Null Renderer filter", 1);
// Add Null Renderer filter to filter graph
hr = pGraph->AddFilter(pNullRenderer, L"NullRender");
if (hr != S_OK)
exit_message("Could not add Null Renderer to filter graph", 1);
// Connect up the filter graph's capture stream
hr = pBuilder->RenderStream(
&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
pCap, pSampleGrabberFilter, pNullRenderer);
if (hr != S_OK)
exit_message("Could not render capture video stream", 1);
hr = pBuilder->RenderStream(
&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
pCap, NULL, NULL);
if (hr != S_OK && hr != VFW_S_NOPREVIEWPIN)
exit_message("Could not render preview video stream", 1);
// Get media control interfaces to graph builder object
hr = pGraph->QueryInterface(IID_IMediaControl,
(void**)&pMediaControl);
if (hr != S_OK) exit_message("Could not get media control interface", 1);
// Run graph
while(1)
{
hr = pMediaControl->Run();
// Hopefully, the return value was S_OK or S_FALSE
if (hr == S_OK) break; // graph is now running
if (hr == S_FALSE) continue; // graph still preparing to run
// If the Run function returned something else,
// there must be a problem
fprintf(stderr, "Error: %u\n", hr);
exit_message("Could not run filter graph", 1);
}
Sleep(2000);
// get the StillImage Pin
hr = pCap->QueryInterface(IID_IAMVideoControl, (void**)&pAMVidControl);
if (hr != S_OK) exit_message("Could not get IAMVideoControl", 1);
hr = pBuilder->FindPin(pCap, PINDIR_OUTPUT, &PIN_CATEGORY_STILL, NULL, FALSE, 0, &pPin);
if (hr != S_OK)
exit_message("Could not get Pin of category StillImage", 1);
hr = pAMVidControl->SetMode(pPin, VideoControlFlag_Trigger);
if (hr != S_OK) exit_message("Could set mode VideoControlFlag_Trigger", 1);
答案 0 :(得分:0)
某些视频捕捉设备源滤镜不会曝光静止图像捕捉图钉。
您是否尝试使用EnumPins方法查找它是否真的有一个,或者是否只有预览引脚?
您也可以使用GraphEdit查看过滤器的引脚。
如果设备只有预览引脚,则必须使用它来获取图像。您可以使用Smart Tee Filter将图表拆分为预览和捕获。