我正在使用 webrtc 和 Threejs 尝试 webAR 演示,同时通过我的 Android 手机 chrome:54 访问相机,前置摄像头默认打开。
我检查并尝试通过参考有关堆栈溢出的所有问题来解决该问题,甚至尝试更改数组值但没有成功。谁能告诉我如何通过调整此代码来访问后置摄像头?
var THREEx = THREEx || {}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
/**
* Grab camera
*/
THREEx.WebcamGrabbing = function() {
// create video element
var domElement = document.createElement('video')
domElement.setAttribute('autoplay', true)
// window.domElement = video
domElement.style.zIndex = -1;
domElement.style.position = 'absolute'
domElement.style.top = '0px'
domElement.style.left = '0px'
domElement.style.width = '100%'
domElement.style.height = '100%'
function onResize() {
// is the size of the video available ?
if (domElement.videoHeight === 0) return
var videoAspect = domElement.videoWidth / domElement.videoHeight
var windowAspect = window.innerWidth / window.innerHeight
}
window.addEventListener('resize', function(event) {
onResize()
})
setInterval(function() {
onResize()
}, 500)
// get the media sources
navigator.mediaDevices.enumerateDevices().then(function(sourceInfos) {
// define getUserMedia() constraints
var constraints = {
video: true,
audio: false,
}
// to mirror the video element when it isnt 'environment'
// domElement.style.transform = 'scaleX(-1)'
// it it finds the videoSource 'environment', modify constraints.video
for (var i = 0; i != sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
if (sourceInfo.kind == "video" && sourceInfo.facing == "environment") {
constraints.video = {
optional: [{
sourceId: sourceInfo.id
}]
}
// not to mirror the video element when it is 'environment'
// domElement.style.transform = ''
}
}
// try to get user media
navigator.getUserMedia(constraints, function(stream) {
domElement.src = URL.createObjectURL(stream);
}, function(error) {
console.error("Cant getUserMedia()! due to ", error);
});
});
this.domElement = domElement
}
我尝试将其添加到约束中。又是同样的结果。
var constraints = {
video: {facingMode: { exact: "environment" }},
audio: false
}
最新的 spac 支持约束中的“faceingMode”。
但是,Chrome 尚不支持“faceingMode”。
“sourceId”已弃用,最新规范“deviceId”取代了“sourceId”。
我写demo.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)