Windows 8 how to choose which camera to initialize

我的未来我决定 提交于 2019-12-23 01:37:42

问题


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;
        }
    })
}


来源:https://stackoverflow.com/questions/16461169/windows-8-how-to-choose-which-camera-to-initialize

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