#include <dshow.h>// 定义宏用于检查DirectShow函数返回值#define SAFE_RELEASE(x) if (x) { x->Release(); x = NULL; }void main(){ // 初始化COM库 CoInitialize(NULL); // 创建Filter Graph Manager对象 IGraphBuilder* pGraphBuilder = NULL; CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void[b])&pGraphBuilder); // 创建Capture Filter对象 ICaptureGraphBuilder2* pCaptureGraphBuilder = NULL; CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void[/b])&pCaptureGraphBuilder); pCaptureGraphBuilder->SetFiltergraph(pGraphBuilder); // 添加摄像头设备到Graph IBaseFilter* pCameraFilter = NULL; pCaptureGraphBuilder->FindInterface(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pCameraFilter, IID_IBaseFilter, (void[b])&pCameraFilter); pGraphBuilder->AddFilter(pCameraFilter, L"Camera Filter"); // 获取视频输入流 IBaseFilter* pVideoInputFilter = NULL; pCaptureGraphBuilder->FindInterface(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCameraFilter, IID_IBaseFilter, (void[/b])&pVideoInputFilter); // 创建Resize Filter对象 IBaseFilter* pResizeFilter = NULL; CoCreateInstance(CLSID_VideoResizerFilter, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void[b])&pResizeFilter); pGraphBuilder->AddFilter(pResizeFilter, L"Resize Filter"); // 连接摄像头输出到Resize Filter输入 pGraphBuilder->ConnectDirect(GetPin(pCameraFilter, PINDIR_OUTPUT), GetPin(pResizeFilter, PINDIR_INPUT), NULL); // 设置Resize Filter的缩放大小属性 IAMVideoProcAmp* pVideoProcAmp = NULL; pVideoProcAmp = GetInterfaceFromFilter<IBaseFilter, IAMVideoProcAmp>(pResizeFilter); if (pVideoProcAmp) { // 设置宽度和高度的缩放比例(例如:0.5缩小一半,2.0放大两倍) long min, max, step, def, flags; pVideoProcAmp->GetRange(VideoProcAmp_Resize, &min, &max, &step, &def, &flags); pVideoProcAmp->Set(VideoProcAmp_Resize, 0.5, flags); } // 渲染Resize Filter的输出 pCaptureGraphBuilder->RenderStream(NULL, NULL, pResizeFilter, NULL, pGraphBuilder); // 启动图形运行 IMediaControl* pMediaControl = NULL; pGraphBuilder->QueryInterface(IID_IMediaControl, (void[/b])&pMediaControl); pMediaControl->Run(); // 等待用户按下任意键停止 system("pause"); // 停止图形运行 pMediaControl->Stop(); // 释放资源 SAFE_RELEASE(pGraphBuilder); SAFE_RELEASE(pCaptureGraphBuilder); SAFE_RELEASE(pCameraFilter); SAFE_RELEASE(pVideoInputFilter); SAFE_RELEASE(pResizeFilter); SAFE_RELEASE(pVideoProcAmp); // 反初始化COM库 CoUninitialize();}
在上述示例代码中,我们创建了一个Filter Graph Manager对象来构建图形,然后使用`ICaptureGraphBuilder2`接口添加摄像头设备到图中。接着,我们创建了一个Resize Filter对象,并将其添加到图中。最后,通过调用`IAMVideoProcAmp`接口的`Set`方法,设置Resize Filter的缩放大小属性。