使用getUserMedia API访问桌面上的摄像头时,会打开网络摄像头。当然,这有助于视频通信。但在移动设备中使用时会调用哪个摄像头。前凸轮或后凸轮?。选择相机需要代码吗?
有一个解决方案,用户可以选择其中一个摄像头。
使用HTML5 启用后置摄像头
通过从以下代码中评估sourceInfo.facing
,您可以选择一个相机("用户"或"环境")(适用于当前chrome>=30):https://simpl.info/getusermedia/sources/
'use strict';
var videoElement = document.querySelector('video');
var audioSelect = document.querySelector('select#audioSource');
var videoSelect = document.querySelector('select#videoSource');
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
function gotSources(sourceInfos) {
for (var i = 0; i !== sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
var option = document.createElement('option');
option.value = sourceInfo.id;
if (sourceInfo.kind === 'audio') {
option.text = sourceInfo.label || 'microphone ' + (audioSelect.length + 1);
audioSelect.appendChild(option);
} else if (sourceInfo.kind === 'video') {
option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log('Some other kind of source: ', sourceInfo);
}
}
}
if (typeof MediaStreamTrack === 'undefined'){
alert('This browser does not support MediaStreamTrack.nnTry Chrome Canary.');
} else {
MediaStreamTrack.getSources(gotSources);
}
function successCallback(stream) {
window.stream = stream; // make stream available to console
videoElement.src = window.URL.createObjectURL(stream);
videoElement.play();
}
function errorCallback(error){
console.log('navigator.getUserMedia error: ', error);
}
function start(){
if (!!window.stream) {
videoElement.src = null;
window.stream.stop();
}
var audioSource = audioSelect.value;
var videoSource = videoSelect.value;
var constraints = {
audio: {
optional: [{sourceId: audioSource}]
},
video: {
optional: [{sourceId: videoSource}]
}
};
navigator.getUserMedia(constraints, successCallback, errorCallback);
}
audioSelect.onchange = start;
videoSelect.onchange = start;
start();
不幸的是,您(还?)无法通过代码选择它。
-
Mozilla Firefox测试版:当用户接受共享相机时,他/她还可以选择要共享的相机。
-
Chrome测试版:我只能使用面部摄像头。可能是可配置,但我不知道如何…
编辑:在chrome中,可以通过编程方式选择背面摄像头。请参阅本线程中Probot的下一个答案。
答案是肯定的,对于Android,默认的摄像头是前置摄像头(<=>用户)。因此,我开发了下面的脚本,以便在前置摄像头和后置摄像头之间进行选择
//----------------------------------------------------------------------
// Here we list all media devices, in order to choose between
// the front camera and the rear camera.
// videoDevices[0] : Front Camera
// videoDevices[1] : Back Camera
// I used an array to save the devices ID
// which I get with using devices.forEach()
// Then I set the video resolution.
//----------------------------------------------------------------------
navigator.mediaDevices.enumerateDevices()
.then(devices => {
var videoDevices = [0,0];
var videoDeviceIndex = 0;
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
if (device.kind == "videoinput") {
videoDevices[videoDeviceIndex++] = device.deviceId;
}
});
var constraints = {width: { min: 1024, ideal: 1280, max: 1920 },
height: { min: 776, ideal: 720, max: 1080 },
deviceId: { exact: videoDevices[1] }
};
return navigator.mediaDevices.getUserMedia({ video: constraints });
})
.then(stream => {
if (window.webkitURL) {
video.src = window.webkitURL.createObjectURL(stream);
localMediaStream = stream;
} else if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
} else if (video.srcObject !== undefined) {
video.srcObject = stream;
} else {
video.src = stream;
}})
.catch(e => console.error(e));