How to get a list of video capture devices (web cameras) on windows? (C++)

前端 未结 2 1629
猫巷女王i
猫巷女王i 2020-11-29 07:43

So we have a simple C++ win32 console app. All we want is to print list of web cameras and other video capture devices that are avaliable. We want to use windows apis as muc

相关标签:
2条回答
  • 2020-11-29 08:09

    How to let user select a video recording device (web-cam) with OpenCV?

    the answer isnt opencv specific

    0 讨论(0)
  • 2020-11-29 08:18

    From the examples shown, copy the following code into dev.c. Then open the command line with all the SDK variables set. At the command line link to ole32.lib and oleaut32.lib. It will then show you all the devices.

    cl dev.c ole32.lib oleaut32.lib

    dev.exe will give out the list on the command line.

    #include <windows.h>
    #include <dshow.h>
    
    #pragma comment(lib, "strmiids")
    
    HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
    {
        // Create the System Device Enumerator.
        ICreateDevEnum *pDevEnum;
        HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,  
            CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
    
        if (SUCCEEDED(hr))
        {
            // Create an enumerator for the category.
            hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);
            if (hr == S_FALSE)
            {
                hr = VFW_E_NOT_FOUND;  // The category is empty. Treat as an error.
            }
            pDevEnum->Release();
        }
        return hr;
    }
    
    
    void DisplayDeviceInformation(IEnumMoniker *pEnum)
    {
        IMoniker *pMoniker = NULL;
    
        while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
        {
            IPropertyBag *pPropBag;
            HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
            if (FAILED(hr))
            {
                pMoniker->Release();
                continue;  
            } 
    
            VARIANT var;
            VariantInit(&var);
    
            // Get description or friendly name.
            hr = pPropBag->Read(L"Description", &var, 0);
            if (FAILED(hr))
            {
                hr = pPropBag->Read(L"FriendlyName", &var, 0);
            }
            if (SUCCEEDED(hr))
            {
                printf("%S\n", var.bstrVal);
                VariantClear(&var); 
            }
    
            hr = pPropBag->Write(L"FriendlyName", &var);
    
            // WaveInID applies only to audio capture devices.
            hr = pPropBag->Read(L"WaveInID", &var, 0);
            if (SUCCEEDED(hr))
            {
                printf("WaveIn ID: %d\n", var.lVal);
                VariantClear(&var); 
            }
    
            hr = pPropBag->Read(L"DevicePath", &var, 0);
            if (SUCCEEDED(hr))
            {
                // The device path is not intended for display.
                printf("Device path: %S\n", var.bstrVal);
                VariantClear(&var); 
            }
    
            pPropBag->Release();
            pMoniker->Release();
        }
    }
    
    void main()
    {
        HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
        if (SUCCEEDED(hr))
        {
            IEnumMoniker *pEnum;
    
            hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
            if (SUCCEEDED(hr))
            {
                DisplayDeviceInformation(pEnum);
                pEnum->Release();
            }
            hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnum);
            if (SUCCEEDED(hr))
            {
                DisplayDeviceInformation(pEnum);
                pEnum->Release();
            }
            CoUninitialize();
        }
    }
    
    0 讨论(0)
提交回复
热议问题