Windows 8 how to choose which camera to initialize

2019-06-02 05:12发布

问题:

I am developing a Windows Store App and I am using the Camera and Microphone capabilities. I would like the back camera to be initialized but the examples that I have found always initialize the front camera. Here's the code that I have:

Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture)
    .done(function (devices) {
        if (devices.length > 0) {
            // Using Windows.Media.Capture.MediaCapture APIs to stream from webcam 
            mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
            mediaCaptureMgr.initializeAsync().done(initializeComplete, initializeError);
        } else {
            var div = document.createElement('div');
            div.innerHTML = "No Camera found";
            document.body.appendChild(div);
        }
    });

In this case mediaCaptureMgr refers to the front camera. I went through the documentation and it says that I have provide a videoDeviceId to the MediaCapture() function like this:

mediaCaptureMgr = new Windows.Media.Capture.MediaCapture({
    videoDeviceId: devices[1].id
});

However still the front camera is initialized. I am writing and testing this on a Surface. Could you help me with this?

回答1:

You have to create a MediaCaptureInitializationSettings object:

var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.videoDeviceId = devices[1].id;
mediaCaptureMgr.initializeAsync(settings).done(initializeComplete, initializeError);


回答2:

Just to complete the "ma_il" correct answer, is not always true that devices[1] is the back camera on devices other than Surface. To test where the camera and other devices are placed you have to check if device information contains other important info as reported on this article: http://msdn.microsoft.com/en-us/library/windows/apps/hh464961.aspx

Complete code should look like this

if (devices.length > 0) {
    devices.forEach(function (currDev) {
        if (currDev.enclosureLocation.panel && currDev.enclosureLocation.panel == Windows.Devices.Enumeration.Panel.back) {
             defaultDeviceId = currDev.id;
        }
    })
}