Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

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

When developing a Universal Application for Windows 10 you are encouraged to detect device specific hardware using IsTypePresent. (Microsoft refer to this feature as 'Light up'). An example from the documentation that checks for a device's back button is:

if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

It is clear here that the string "Windows.Phone.UI.Input.HardwareButtons" is passed as an argument to the IsTypePresent() method.

I would like to know if there is an easy way to identify other strings that I could use for other pieces of hardware and, in particular, the camera.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...