• C++获取电脑上连接的多个摄像头名称与编号


    #include<iostream>
    #include "strmif.h"
    #include <initguid.h>
    #include<vector>
    #include<string>

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

    using namespace std;

    #define VI_MAX_CAMERAS 20
    DEFINE_GUID(CLSID_SystemDeviceEnum, 0x62be5d10, 0x60eb, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
    DEFINE_GUID(CLSID_VideoInputDeviceCategory, 0x860bb310, 0x5d01, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
    DEFINE_GUID(IID_ICreateDevEnum, 0x29840822, 0x5b84, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);

    //列出硬件设备
    int listDevices(vector<string>& list)
    {
      ICreateDevEnum *pDevEnum = NULL;
      IEnumMoniker *pEnum = NULL;
      int deviceCounter = 0;
      CoInitialize(NULL);

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

      if (SUCCEEDED(hr))
      {
        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.
            }

            VARIANT varName;
            VariantInit(&varName);
            hr = pPropBag->Read(L"Description", &varName, 0);
            if (FAILED(hr))
            {
              hr = pPropBag->Read(L"FriendlyName", &varName, 0);
            }

            if (SUCCEEDED(hr))
            {
              hr = pPropBag->Read(L"FriendlyName", &varName, 0);
              int count = 0;
              char tmp[255] = { 0 };
              while (varName.bstrVal[count] != 0x00 && count < 255)
              {
                tmp[count] = (char)varName.bstrVal[count];
                count++;
              }
              list.push_back(tmp);
            }

            pPropBag->Release();
            pPropBag = NULL;
            pMoniker->Release();
            pMoniker = NULL;

            deviceCounter++;
          }

          pDevEnum->Release();
          pDevEnum = NULL;
          pEnum->Release();
          pEnum = NULL;
        }
      }
      return deviceCounter;
    }

    int main()
    {
      vector<string> CameraName;//存储摄像头名称
      int num = listDevices(CameraName);
      cout << "摄像头个数:" << num << endl;
      for (int i = 0; i < num; i++) {
        cout << " ID: "<< i << " : " << " Name: "<<CameraName[i] << endl;
      }

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

     ===================运行效果===============

     代码复制粘贴即可运行

  • 相关阅读:
    Win Oracle 监听文件配置参考
    Win Tomcat8 占用内存过高
    Centos6.X创建Oracle用户
    js脚本中执行java后台代码
    dom4j递归解析XML字符串所有子节点
    加载Properties配置文件
    Servlet里面request处理外部POST请求的输入流的工具类
    JAVA接口测试类
    Android、iOS与Servlet接口上传文件和JSON串的交互
    FileUpload之FileItem类的常用方法
  • 原文地址:https://www.cnblogs.com/herd/p/9277402.html
Copyright © 2020-2023  润新知