How to detect is a camera is available using isTypePresent in a Windows 10 Universal Application

邮差的信 提交于 2019-11-29 07:40:45

IsTypePresent isn't used to detect hardware presence but to detect API presence. In your code snippet it's checking if the HardwareButtons class exists for the app to call, not if the device has hardware buttons (in this case they're likely to go together, but that's not what IsTypePresent is looking for).

The MediaCapture class used with the camera is part of the Universal API Contract and so is always there and callable. Initialization will fail if there is no appropriate audio or video device.

To find hardware devices you can use the Windows.Devices.Enumeration namespace. Here's a quick snippet that queries for cameras and finds the ID of the first one.

var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);

if (devices.Count < 1)
{
    // There is no camera. Real code should do something smart here.
    return;
}

// Default to the first device we found
// We could look at properties like EnclosureLocation or Name
// if we wanted a specific camera
string deviceID = devices[0].Id;

// Go do something with that device, like start capturing!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!