1

我想创建一个过滤器图,它允许我从不应渲染的(网络摄像头)视频流中获取图像样本。

我已经创建了一个过滤器图,它允许我预览我的网络摄像头视频流并从中获取图像样本。所以我只需要不渲染视频流。

据我所知,我应该使用 NullFilter 来处理视频流输出,但我没有成功使用它......

这是我到目前为止所拥有的:

IGraphBuilder           *pGraph;
ICaptureGraphBuilder2   *pBuild;
IMediaControl           *pControl;
IMediaEvent             *pEvent;
IMoniker                *pVideoSel; 
IBaseFilter             *pVCap;
IBaseFilter             *pGrabberFilter;
IBaseFilter             *pNullRenderer;
ISampleGrabber          *pGrabber;
IVideoWindow            *pVidWin;

过滤器图初始化:

Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

// COM library initialization
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);    // same as CoInitialize but for all the threads

// Create the Capture Graph Builder.
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pBuild );

// Create the Filter Graph Manager.
hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&pGraph);
// Initialize the Filter Graph  for the Capture Graph Builder to use.
pBuild->SetFiltergraph(pGraph);

// Attach the graph control
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
// Attach the graph events
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

添加相机:

HRESULT hr;

//Device binding with connection
hr = pVideoSel->BindToObject(0, 0, IID_IBaseFilter, (void**)&pVCap);

// Add the Device Filter to the Graph
hr = pGraph->AddFilter(pVCap, L"Video Capture");

//Creates Grabber Filter and adds it to the Filter Graph
//Once connected, Grabber Filter will capture still images
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGrabberFilter));

hr = pGraph->AddFilter(pGrabberFilter, L"Sample Grabber");
hr = pGrabberFilter->QueryInterface(IID_PPV_ARGS(&pGrabber));

AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hr = pGrabber->SetMediaType(&mt);
hr = pGrabber->SetOneShot(FALSE);
hr = pGrabber->SetBufferSamples(TRUE);;

//Channels the camera output to GrabberFilter
hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pVCap, pGrabberFilter, NULL);

hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);

pVidWin->put_Owner((OAHWND)hWnd);       // We own the window now
pVidWin->put_WindowStyle(WS_CHILD);     // you are now a child

pVidWin->SetWindowPosition(img_position.left, img_position.top, rc.right, rc.bottom);

pControl->Run();

然后我可以通过以下方式获取我的图像样本:

long pBufferSize = 0;               // pBuffer size
long Size = 0;
hr = mydata1->pGrabber->GetCurrentBuffer(&Size, NULL);
if (FAILED(hr))
    return 0;
else if (Size != pBufferSize) {
    pBufferSize = Size;
    if (pBuffer != 0)
        delete[] pBuffer;
    pBuffer = new unsigned char[pBufferSize];
}
hr = mydata1->pGrabber->GetCurrentBuffer(&pBufferSize, (long*)pBuffer);

.

要删除我尝试删除的视频流渲染:

hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pVCap, pGrabberFilter, NULL);

hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);

pVidWin->put_Owner((OAHWND)hWnd);       // We own the window now
pVidWin->put_WindowStyle(WS_CHILD);     // you are now a child

pVidWin->SetWindowPosition(img_position.left, img_position.top, rc.right, rc.bottom);

并添加:

GUID majorType = MEDIATYPE_Video;
hr = Main_graph->pBuild->RenderStream (NULL, &majorType, pVCap, NULL, pGrabberFilter);
hr = Main_graph->pBuild->RenderStream (NULL, &majorType, pGrabber, NULL, pNullRenderer);

但是当我尝试mydata1->pGrabber->GetCurrentBuffer(&pBufferSize, (long*)pBuffer);它失败了......

关于我做错了什么的任何想法?

4

0 回答 0