我正在开发Windows Store App
,我正在使用Camera
和Microphone
功能。我想要对后置摄像头进行初始化,但我发现的示例总是初始化前置摄像头。这是我的代码:
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);
}
});
在这种情况下,mediaCaptureMgr
指的是前置摄像头。我浏览了文档,并说我已经向videoDeviceId
函数提供MediaCapture()
,如下所示:
mediaCaptureMgr = new Windows.Media.Capture.MediaCapture({
videoDeviceId: devices[1].id
});
然而,前置摄像头仍然被初始化。我正在Surface上编写和测试它。你能帮我解决这个问题吗?
答案 0 :(得分: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;
}
})
}
答案 1 :(得分:3)
您必须创建一个MediaCaptureInitializationSettings
对象:
var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.videoDeviceId = devices[1].id;
mediaCaptureMgr.initializeAsync(settings).done(initializeComplete, initializeError);