gpt4 book ai didi

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

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

我正在尝试使用 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();
}

关于我做错了什么以及我必须做什么才能让它发挥作用有什么想法吗?

最佳答案

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* 转换为无效**

关于c++ - 如何使用 DirectShow 和网络摄像头预览图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23892885/

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