在C++中使用DirectShow处理多个摄像头通常涉及创建多个Filter Graph,每个摄像头一个。以下是一个简化的代码示例,展示了如何为两个摄像头创建两个独立的Filter Graph:
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <dshow.h> #pragma comment(lib, "strmiids") HRESULT CreateCameraGraph(int cameraIndex) { IGraphBuilder *pGraph = NULL; ICaptureGraphBuilder2 *pBuild = NULL; IBaseFilter *pCamera = NULL; IBaseFilter *pFilter = NULL; IMediaControl *pControl = NULL; IVideoWindow *pVideoWindow = NULL; IMoniker *pMoniker = NULL; IEnumMonikers *pEnum = NULL; IEnumFilters *pEnumFilters = NULL; // 初始化COM库 CoInitialize(NULL); // 创建组件 CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph); CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pBuild); // 设置捕获图构建器的图形 pBuild->SetFiltergraph(pGraph); // 创建类别和枚举器 CLSID clsid; CLR_VIDEO_DEVICE_CATEGORY category = CLR_VIDEO_DEVICE_CATEGORY_CAMERA; HRESULT hr = CLR_GetVideoDeviceFilterCLSID(category, &clsid); if (FAILED(hr)) { // 错误处理 } // 创建摄像头过滤器 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&pCamera); if (FAILED(hr)) { // 错误处理 } // 将摄像头添加到图形 hr = pGraph->AddFilter(pCamera, L"Camera"); if (FAILED(hr)) { // 错误处理 } // 此处省略其他代码,如设置Filter Graph的其他组件,以及控制摄像头捕获等... // 最后,当不再需要时,释放接口 pVideoWindow->Release(); pControl->Release(); pGraph->Release(); pBuild->Release(); pCamera->Release(); return S_OK; } int main() { // 创建第1个摄像头的Filter Graph HRESULT hr = CreateCameraGraph(0); if (FAILED(hr)) { // 错误处理 } // 创建第2个摄像头的Filter Graph hr = CreateCameraGraph(1); if (FAILED(hr)) { // 错误处理 } // 注意:实际应用中可能还需要处理线程同步和资源管理 return 0; }
|
这个示例代码提供了创建两个摄像头Filter Graph的基本框架。在实际应用中,你需要处理例如Filter Graph的构建、控制摄像头捕获、多线程处理以及资源管理等更复杂的细节。
请注意,上述代码中的CLR_GetVideoDeviceFilterCLSID
和CLR_VIDEO_DEVICE_CATEGORY
是示例性质的,可能需要根据你的DirectShow实现进行相应的替换或修改。