在Windows中遍历系统摄像头

-- TOC --

网络上找到的一段代码,可以实现遍历Windows系统的所有摄像头,测试结果OK。

这段代码与opencv无关,经过个人修改再测试,如下:

#include "windows.h"
#include "dshow.h"
#include <iostream>
#include <vector>
using namespace std;

#pragma comment(lib, "strmiids.lib")
//#pragma comment(lib, "quartz.lib")


 void listDevices(vector<string>& devlist) {
     ICreateDevEnum* pDevEnum = NULL;
     IEnumMoniker* pEnum = NULL;

     CoInitialize(NULL);
     HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
         CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
         reinterpret_cast<void**>(&pDevEnum));

     if (SUCCEEDED(hr)) {
         // Create an enumerator for the video capture category.
         hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
         if (hr == S_OK) {
             IMoniker* pMoniker = NULL;
             while (pEnum->Next(1, &pMoniker, NULL) == S_OK) {
                 IPropertyBag* pPropBag;
                 hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)(&pPropBag));
                 if (FAILED(hr)) {
                     pMoniker->Release();
                     continue;  // Skip this one, maybe the next one will work.
                 }

                 // Find the description or friendly name.
                 VARIANT varName;
                 VariantInit(&varName);
                 bool read_success = false;
                 hr = pPropBag->Read(L"FriendlyName", &varName, 0);
                 if (FAILED(hr)) {
                     hr = pPropBag->Read(L"Description", &varName, 0);
                     if (FAILED(hr))
                         read_success = false;
                     else
                         read_success = true;
                 }
                 else
                     read_success = true;

                 if (read_success){
                     int count = 0;
                     char tmp[255] = { 0 };
                     while (varName.bstrVal[count] != 0x00 && count < 255) {
                        tmp[count] = (char)varName.bstrVal[count];
                        count++;
                     }
                     devlist.push_back(tmp);
                 }
                 pPropBag->Release();
                 pPropBag = NULL;
                 pMoniker->Release();
                 pMoniker = NULL;
             }
             pDevEnum->Release();
             pDevEnum = NULL;
             pEnum->Release();
             pEnum = NULL;
         }
     }

     return;
 }


 int main(void) {
    vector<string> devlist;

    cout << "Looking for capture devices:" << endl;
    listDevices(devlist);
    cout << "foudn dev number = " << devlist.size() << endl;
    for (int i = 0; i < devlist.size(); ++i) {
        cout << "device name: " << devlist[i] << ", " << "index=" << i << endl;
    }

    //system("pause");
    return 0;
}

这是我编译好的版本:camera_scan_x64.zip

运行效果:

D:\>camera_scan_x64.exe
Looking for capture devices:
foudn dev number = 2
device name: USB2.0 PC CAMERA, index=0
device name: OBS Virtual Camera, index=1

本文链接:https://cs.pynote.net/sf/win/202209225/

-- EOF --

-- MORE --