Directshow-无法使用SetMode

时间:2018-08-30 13:08:19

标签: c++ directshow

我正在尝试使用DirectShow(相机制造商建议使用的框架)从相机的静止销中获取图像。

我一直在关注https://docs.microsoft.com/en-gb/windows/desktop/DirectShow/capturing-an-image-from-a-still-image-pin上的DirectShow MS文档,尽管我可以使用ICaptureGraphBuilder2 :: FindPin方法找到静止销,并且已经使用graphedit应用程序确认了销在相机上,但我已经使用IAMVideoControl :: SetMode方法更改引脚以撞墙。

我正在使用的代码如下:

HRESULT hr = CoInitializeEx(NULL, COINIT::COINIT_MULTITHREADED);
if (FAILED(hr))
    Console::WriteLine("Initialise Failed");
else
    Console::WriteLine("Initialise Success");


//Initalise graph builder
ICaptureGraphBuilder2 *pBuild;
IGraphBuilder *pGraph;

hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL,
    CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void **)&pBuild);
if (FAILED(hr))
    Console::WriteLine("Graph builder failed");
else
    Console::WriteLine("Graph builder success");

//Create filter graph
hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER,
    IID_IGraphBuilder, (void**)&pGraph);
if (SUCCEEDED(hr))
{
    pBuild->SetFiltergraph(pGraph);
    Console::WriteLine("Set filter graph success");
}
else
{
    pBuild->Release();
    Console::WriteLine("Set filter graph failed");
}


// Create the System Device Enumerator.
ICreateDevEnum *pDevEnum;
IEnumMoniker *pDevicesInfo;
HRESULT hr2 = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
    CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));

if (SUCCEEDED(hr2))
{
    Console::WriteLine("Device enum builder success");

    // Create an enumerator for the video category.
    hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pDevicesInfo, 0);
    if (hr == S_FALSE)
        Console::WriteLine("Retrieve video input devices failed");
    else
        Console::WriteLine("Retrieve video input devices success");
    pDevEnum->Release();
}

//loop over video devices and find see3cam_130

IMoniker *pMoniker = NULL;

while (pDevicesInfo->Next(1, &pMoniker, NULL) == S_OK)
{
    IPropertyBag *pPropBag;
    HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
    if (FAILED(hr))
    {
        pMoniker->Release();
        continue;
    }

    VARIANT var;
    VariantInit(&var);

    // Get description or friendly name.
    hr = pPropBag->Read(L"FriendlyName", &var, 0);
    if (SUCCEEDED(hr))
    {

        Console::WriteLine("Found video device");
        //If correct device, break loop
        if (0 == wcscmp(var.bstrVal, L"See3CAM_130"))
        {
            Console::WriteLine("See3CAM_130");
            break;
        }
        else
        {
            Console::WriteLine("Comparison failed");
        }

        printf("%S\n", var.bstrVal);
        VariantClear(&var);
    }
    else
    {
        Console::WriteLine("Vid Device failed");
    }

}
//Create filter for device
IBaseFilter *pCap = NULL;
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCap);
if (SUCCEEDED(hr))
{
    Console::WriteLine("Filter created");
    hr = pGraph->AddFilter(pCap, L"Capture Filter");
    if (SUCCEEDED(hr))
    {
        Console::WriteLine("Capture filter added to filter graph");
    }
    else
    {
        Console::WriteLine("Capture filter NOT added to filter graph");
    }
}
else
    Console::WriteLine("Filter failed");

//Render capture pin first before attempting to set still pin (setting still pin fails with ot without this step)
hr = pBuild->RenderStream(
    &PIN_CATEGORY_CAPTURE, // Pin category.
    &MEDIATYPE_Video,      // Media type.
    pCap,                  // Capture filter.
    NULL,                  // Intermediate filter (optional).
    NULL);                 // Mux or file sink filter.
if (SUCCEEDED(hr))
    Console::WriteLine("Stream Render success");
else
    Console::WriteLine("Stream Render failed");



//Create a control to run graph
IMediaControl *pControl;

hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
if (FAILED(hr))
    Console::WriteLine("Graph initalised - Failed");
else
    Console::WriteLine("Graph initalised - Success");


// Run the graph.
while (1)
{
    hr = pControl->Run(); 
    if (FAILED(hr))
        Console::WriteLine("Graph run - Failed");
    else
    {
        Console::WriteLine("Graph run - Success");
        break;
    }

}

//Find the still pin
IAMVideoControl *pAMVidControl = NULL;
hr = pCap->QueryInterface(IID_IAMVideoControl, (void**)&pAMVidControl);

if (SUCCEEDED(hr))
{
    Console::WriteLine("Video Control Initialised - Success");


    IPin *pPin = NULL;

    hr = pBuild->FindPin(
        pCap,                  // Filter.
        PINDIR_OUTPUT,         // Look for an output pin.
        &PIN_CATEGORY_STILL,   // Pin category.
        NULL,                  // Media type (don't care).
        FALSE,                 // Pin must be unconnected.
        0,                     // Get the 0'th pin.
        &pPin                  // Receives a pointer to thepin.
        );

    if (SUCCEEDED(hr))
    {
        Console::WriteLine("Find pin - Success");

        hr = pAMVidControl->SetMode(pPin, VideoControlFlag_Trigger);

        if (FAILED(hr))
            Console::WriteLine("Pin Mode Set - Failed");
        else
            Console::WriteLine("Pin Mode Set - Success");

        pPin->Release();
    }
    else
        Console::WriteLine("Find pin - Failed");

}
else
    Console::WriteLine("Video Control Initialised - Failed");

一切都成功了,直到我尝试使用SetMode方法并且不知道为什么...

我还是一个初级开发人员,并且对c ++缺乏经验,所以我怀疑我错过了某个地方。我已经考虑过使用directshow.net包装器,但是我不想浪费我花的时间。

关于IAMVideoControl :: SetMode方法为什么在我的代码中失败的任何提示?

注意:HResult错误为ERROR_GEN_FAILURE(可用于指示设备已停止响应(挂起)或设备发生一般故障。设备可能需要手动重置。)

1 个答案:

答案 0 :(得分:0)

我已经看到您查询了,您必须做很多工作才能捕获静止帧。

枚举静态图钉之后,应连接示例采集卡,并将渲染为空的值插入相机的静态图钉。

构建并运行图形后,您应该触发静止模式,并且帧将在抓取器中接收。

然后,您可以随心所欲地使用相框。