Windows 8如何选择初始化哪个摄像头

Kon*_*nev 1 javascript camera windows-8 winjs

我正在开发一个Windows Store App,我正在使用CameraMicrophone功能.我想要对后置摄像头进行初始化,但我发现的示例总是初始化前置摄像头.这是我的代码:

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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

在这种情况下mediaCaptureMgr指的是前置摄像头.我浏览了文档,它说我已经提供了videoDeviceId这样的MediaCapture()函数:

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

然而,前置摄像头仍然被初始化.我正在Surface上编写和测试它.你能帮帮我吗?

zaf*_*ste 6

只是为了完成"ma_il"正确答案,并不总是设备[1]是除Surface之外的设备上的后置摄像头.要测试相机和其他设备的放置位置,您必须检查设备信息是否包含本文中报告的其他重要信息:http: //msdn.microsoft.com/en-us/library/windows/apps/hh464961.aspx

完整代码应如下所示

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