gpt4 book ai didi

c++ - 将 SampleGrabber 与 CaptureGraphBuilder2 结合使用时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:46 25 4
gpt4 key购买 nike

我一直在尝试使用 SampleGrabber 从网络摄像头抓取帧并将其保存为位图,但一直没有成功。我使用 ICaptureGraphBuilder2 在 MSDN 上使用了 Directshow 视频捕获示例以及示例抓取器示例。

我希望我的过滤图看起来像下面这样:

网络摄像头(来源)-> 样本采集器-> 空渲染器

由于我对预览不感兴趣,所以我认为空渲染器对我来说就足够了。

计划是通过搜索特定的 VID 和 PID (webcamCapture::findWebCam()) 来找到我的网络摄像头,创建我的样本采集器,创建空渲染器,然后使用 pGraphBuilder->RenderStream() 将它们连接在一起。由于我只需要一帧,因此我使用了 OneShot 方法,然后使用 GetCurrentBuffer 来检索帧。

问题出现在这一行:

pEvent->WaitForCompletion(2000, &evCode);

返回的 HRESULT 代码是 E_ABORT。将超时设置为 INFINITE 会导致程序卡住,这可能意味着过滤器永远不会停止。 pControl->Run() 的 HRESULT 是 S_FALSE,这意味着并非所有过滤器都在运行。我不确定为什么会这样,我感觉在构建这些过滤器时出了点问题。

下面是我的来源转储。我创建了一个类来处理网络摄像头捕获。在使用中,基本上有两件事被调用:

webcamCapture::buildFilterGraph()
webcamCapture::runFilter()

runFilter 将运行过滤器并将帧保存为位图文件。我是 directshow 的新手,我很确定我错过了一些简单的东西。任何见解将不胜感激!

来源

    HRESULT webcamCapture::buildFilterGraph(void){
HRESULT hr;

if (FAILED(hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
return hr;
}

createCOMInstances();

// Use findWebCam() to find the webcam and add it to our capture graph
hr = findWebCam();
if(FAILED(hr)){
return hr;
}

// Add the sample grabber to the graph
hr = pGraph->AddFilter(pGrabberF, L"Sample Grabber");
if(FAILED(hr)){
return hr;
}

hr = pGrabberF->QueryInterface(IID_PPV_ARGS(&pGrabber));
if(FAILED(hr)){
return hr;
}

// Set media type for the sample grabber (24-bit RGB Uncompressed video)
ZeroMemory(&mt, sizeof(mt));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;

hr = pGrabber->SetMediaType(&mt);
if(FAILED(hr)){
return hr;
}

hr = pGraph->AddFilter(pNullF, L"Null Filter");
if(FAILED(hr)){
return hr;
}

// Connect source (pCapF), sample grabber (pGrabberF) and null renderer (pNullF)
pGraphBuilder->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pCapF,pGrabberF, pNullF);

hr = pGrabber->SetOneShot(TRUE);
hr = pGrabber->SetBufferSamples(TRUE);


return S_OK;
}

HRESULT webcamCapture::runFilter(){
HRESULT hr;
FILTER_STATE filterState;

pBuffer = NULL;



hr = pControl->Run();
if(FAILED(hr)){
goto done;
}

pControl->GetState(1000, (OAFilterState*)&filterState);

long evCode;
hr = pEvent->WaitForCompletion(2000, &evCode);
if(FAILED(hr)){
goto done;
}

long cbBuffer;
hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);
if(FAILED(hr)){
goto done;
}

pBuffer = (BYTE*)CoTaskMemAlloc(cbBuffer);
if(!pBuffer){
hr = E_OUTOFMEMORY;
goto done;
}

hr = pGrabber->GetCurrentBuffer(&cbBuffer, (long*)pBuffer);
if(FAILED(hr)){
goto done;
}

hr = pGrabber->GetConnectedMediaType(&mt);
if(FAILED(hr)){
goto done;
}

if((mt.formattype == FORMAT_VideoInfo) && (mt.cbFormat >= sizeof(VIDEOINFOHEADER)) && (mt.pbFormat != NULL)){

VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)mt.pbFormat;
writeBitmap(L"test_image.bmp", &pVih->bmiHeader, mt.cbFormat - SIZE_PREHEADER, pBuffer, cbBuffer);
}

//FreeMediaType(mt);

done:
CoTaskMemFree(pBuffer);
pNullF->Release();
pNullF = NULL;
pCapF->Release();
pCapF = NULL;
pGrabber->Release();
pGrabber = NULL;
pGrabberF->Release();
pGrabberF = NULL;
pControl->Release();
pControl = NULL;
pEvent->Release();
pEvent = NULL;
pGraph->Release();
pGraph = NULL;
CoUninitialize();


return hr;
}

HRESULT webcamCapture::createCOMInstances(void){
HRESULT hr;

// Instructions to create a Capture Graph using CaptureGraphBuilder2 can be found at https://msdn.microsoft.com/en-us/library/windows/desktop/dd373396(v=vs.85).aspx

// Create Capture Graph Builder
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGraphBuilder));
if(FAILED(hr)){
return hr;
}

// Create Graph Manager
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGraph));
if(FAILED(hr)){
return hr;
}

// Initialize Capture Graph Builder by referencing pGraph (Graph Manager)
hr = pGraphBuilder->SetFiltergraph(pGraph);
if(FAILED(hr)){
pGraphBuilder->Release();
return hr;
}

// Set pointer to Media Control Interface
hr = pGraph->QueryInterface(IID_PPV_ARGS(&pControl));
if(FAILED(hr)){
return hr;
}

// Set pointer to Media Event Interface
hr = pGraph->QueryInterface(IID_PPV_ARGS(&pEvent));
if(FAILED(hr)){
return hr;
}

// Create Sample Grabber
// NOTE: ISampleGrabber is depreciated and may not be supported in later versions of Windows!
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGrabberF));
if(FAILED(hr)){
return hr;
}

// Create the null renderer filter. We don't be previewing the camera feed so we can just drop the frames when we've converted them to bitmaps
hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pNullF));
if(FAILED(hr)){
return hr;
}

TRACE(_T("Interface Creation Successful\n"));
return S_OK;
}

// Function to find the MS HD3000 webcam (VID=0x045E PID=0x0779)
// For more info on finding devices for Video/Audio capture, see https://msdn.microsoft.com/en-us/library/windows/desktop/dd377566(v=vs.85).aspx
HRESULT webcamCapture::findWebCam(void){

HRESULT hr;
ICreateDevEnum *pDevEnum;
IEnumMoniker *pEnum;
IMoniker *pMoniker;
BOOL isDeviceFound = FALSE;

WORD wNumCameras = 0;

hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
if(FAILED(hr)){
return hr;
}

hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
if(hr == S_FALSE){
return VFW_E_NOT_FOUND;
}

pDevEnum->Release();

// Go through each device moniker and read their Device Path properties. This is how we will find our webcam of interest
while(pEnum->Next(1, &pMoniker, NULL) == S_OK){

IPropertyBag *pPropBag;
VARIANT var;

hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));

if(FAILED(hr)){
pMoniker->Release();
continue;
}

VariantInit(&var);

hr = pPropBag->Read(L"DevicePath", &var, 0);
if(SUCCEEDED(hr)){
// String search variables
CString csVidToCompare = _T("");
CString csPidToCompare = _T("");
WORD wVidSearchIndex = 0;
WORD wPidSearchIndex = 0;

CString csDevPath = var.bstrVal;
csDevPath.MakeLower();

wVidSearchIndex = csDevPath.Find(_T("vid_"), 0);
wPidSearchIndex = csDevPath.Find(_T("pid_"), 0);

csVidToCompare = csDevPath.Mid(wVidSearchIndex + 4, 4);
csPidToCompare = csDevPath.Mid(wPidSearchIndex + 4, 4);

// If MS Device is found
if(!csVidToCompare.Compare(_T("045e"))){
// If the 3000HD camera is found
if((!csPidToCompare.Compare(_T("0779"))) && (wNumCameras == 0)){
wNumCameras++;
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCapF);
if(FAILED(hr)){
return hr;
}

isDeviceFound = TRUE;
// Add the webcam to the filter graph
hr = pGraph->AddFilter(pCapF, L"Capture Filter");

if(FAILED(hr)){
TRACE(_T("Failed to add webcam to filter graph\n"));
return hr;
}

TRACE(_T("Webcam found and added to filter!\n"));

}

else if((!csPidToCompare.Compare(_T("0779"))) && (wNumCameras > 0)){
TRACE(_T("More than one HD3000 camera found!\n"));
continue;
}

else{
TRACE(_T("MS Device found, but not the camera\n"));
continue;
}
}
}

pPropBag->Release();
pMoniker->Release();
}

if(isDeviceFound == FALSE){
TRACE(_T("Webcam was not found\n"));
pEnum->Release();
return E_FAIL;
}

pEnum->Release();

return S_OK;
}

最佳答案

代码是正确的。是的,您可以像这样构建一个图形,单次拍摄模式下的样本采集器接受视频帧并指示完成。

潜在问题 list 包括:

  1. 您的视频设备不提供帧,采样器将永远等待
  2. 您请求转换为 24 位 RGB,并且您的相机更可能支持其他视频格式,因此您可能为您插入了另一个转换器过滤器 - 视频帧可能会离开相机过滤器并在转换器中丢失;您有兴趣查看您构建的有效图表
  3. 你构建的图表不正确,你的样本采集器实际上没有正确连接

在任何情况下,您要做的第一件事就是简单地进入调试器并检查线程,您可能会当场发现错误。那么你要做的第二件事就是找出如何review your filter graphs .这是任何 DirectShow 开发所必需的。

S_FALSE 返回对于在拓扑中具有实时源的图是典型的。这很好。

关于c++ - 将 SampleGrabber 与 CaptureGraphBuilder2 结合使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com