How to select video device to initialize using mediaCapture.InitializeAsync()?

738 Views Asked by At

I am trying to make a simple image capture app using C# in Visual Studio 2019.

I want to use an external USB webcam to capture an image, but when I try to initialize the device, I realized I have to distinguish the USB webcam from my built-in webcam on my laptop.

// Finds all video capture devices
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

foreach (var device in devices)
{
    // I want to filter my USB Webcam and set it aside from my built-in laptop webcam
    
}

In some other posts, they distinguish it using panel orientation

foreach (var device in devices)
{
    switch(device.EnclosureLocation.Panel)
    {
    case Windows.Devices.Enumeration.Panel.Front:
        frontCamera = device; //frontCamera is of type DeviceInformation
        isUsingFrontCam = true;
        break;
    case Windows.Devices.Enumeration.Panel.Back:
        rearCamera = device; //rearCamera is of type DeviceInformation
        break;
    default:
        //you can also check for Top, Left, right and Bottom
        break;
    }
}

But in my case, I am using USB Webcam so I don't think there is any orientation. Any suggestions? Thank you!

1

There are 1 best solutions below

0
On

As one of solutions, you can use device ID like below.

string deviceId = string.Empty;

// Find all video capture devices
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            foreach (var device in devices)
            {
                // Filter by Vendor ID
                if (device.Id.Contains("#VID_Whatever"))
                {
                    deviceId = device.Id;
                    break;
                }
            }
            if (deviceId == string.Empty)
                throw new Exception("No target devices found");