如何使用DirectShow和网络摄像头预览图像

时间:2014-05-27 15:05:36

标签: c++ webcam directshow

我正在尝试使用C ++和DirectShow来显示我的网络摄像头......但我遇到了一些麻烦。 以下代码给出了一个分段错误:

m_pDF->EnumPins(&pinEnum);

我的cpp代码:

#include <tchar.h>
#include <strsafe.h>
#include <dshow.h>
#include <atlbase.h>
#include <d3d9.h>
#include <vmr9.h>

#pragma comment(lib,"Strmiids.lib")

#define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }

HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
{
    // Create the System Device Enumerator.
    ICreateDevEnum *pDevEnum;   // Video and Audio interface object
    HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); // Creates the system device enumerator

    if (SUCCEEDED(hr))
    {
        // Create an enumerator for the category.
        hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);  // Enumeration of 'category' objects
        if (hr == S_FALSE)
        {
            hr = VFW_E_NOT_FOUND;  // The category is empty. Treat as an error.
        }
        pDevEnum->Release(); // Deletes Enumeration object
    }
    return hr;
}

void main(void)
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL; // contains methods for stopping and starting the graph
    IMediaEvent   *pEvent = NULL;   // methods for getting events from the Filter Graph Manager

    IPin *m_pCamOutPin;
    IBaseFilter *m_pDF=NULL;
    IMoniker *pM;
    IEnumMoniker *pEnum; // Enumerator object

    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return;
    }

    // Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return;
    }

    // Bind
    hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
    pEnum->Next(1, &pM, NULL);
    pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);
    pM->Release();

    hr=pGraph->AddFilter(m_pDF, L"Video Capture");

    CComPtr<IEnumPins> pinEnum;
    m_pDF->EnumPins(&pinEnum);

    hr = pinEnum->Reset();
    hr = pinEnum->Next(1, &m_pCamOutPin, NULL); 

    if (FAILED(hr))
    return;

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

    // Build the graph. IMPORTANT: Change this string to a file on your system.
    hr = pGraph->Render(m_pCamOutPin);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    SAFE_RELEASE(pControl);
    SAFE_RELEASE(pEvent);
    SAFE_RELEASE(pGraph);
    CoUninitialize();
}

关于我做错了什么以及我需要做些什么才能让它发挥作用的任何想法?

2 个答案:

答案 0 :(得分:1)

m_pDF是指向IBaseFilter的指针:

IBaseFilter *m_pDF=NULL;

BindToObject最后一个参数是指向指针的指针。这意味着这个功能错误:

pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);

相反,您需要传递m_pDF的地址以获取指向指针的指针:

pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&m_pDF);

IBaseFilter**投射到void**并不是问题,但您无法将IBaseFilter*投射到void**

答案 1 :(得分:0)

崩溃应该与m_pDF相关,它不是有效的指针。

您应该添加pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);的检查,以了解m_pDF是否初始化成功。

您还应该检查hr=pGraph->AddFilter(m_pDF, L"Video Capture"); hr是否正常。

如果失败,你可以得到错误代码知道原因。