我目前正在使用安卓平板电脑和GetUserMedia在我的程序中拍照。
显然,GetUserMedia使用的默认摄像头是前置摄像头。如何使用后置摄像头作为默认设置?
下面是我的GetUserMedia代码:
navigator.getUserMedia({
"audio": false,
"video": {
mandatory: {
minWidth: this.params.dest_width,
minHeight: this.params.dest_height,
//facingMode: "environment",
},
}
},
function(stream) {
// got access, attach stream to video
video.src = window.URL.createObjectURL( stream ) || stream;
Webcam.stream = stream;
Webcam.loaded = true;
Webcam.live = true;
Webcam.dispatch('load');
Webcam.dispatch('live');
Webcam.flip();
},
function(err) {
return self.dispatch('error', "Could not access webcam.");
});我在“强制”部分插入了facingMode,但没有起作用。
请帮帮忙。
发布于 2015-09-03 07:48:37
更新: facingMode现在可以通过adapter.js polyfill在安卓浏览器中使用!
facingMode是not yet implemented in Chrome for Android,但在安卓的火狐中原生工作。
但是,您必须使用standard约束:(对Chrome使用https fiddle ):
var gum = mode =>
navigator.mediaDevices.getUserMedia({video: {facingMode: {exact: mode}}})
.then(stream => (video.srcObject = stream))
.catch(e => log(e));
var stop = () => video.srcObject && video.srcObject.getTracks().forEach(t => t.stop());
var log = msg => div.innerHTML += msg + "<br>";<button onclick="stop();gum('user')">Front</button>
<button onclick="stop();gum('environment')">Back</button>
<div id="div"></div><br>
<video id="video" height="320" autoplay></video>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
{ exact: }语法意味着约束是必需的,如果用户没有正确的摄像头,事情就会失败。如果你忽略了它,那么这个约束就是可选的,这在Firefox for Android中意味着它只会在权限提示符中更改摄像头选择器中的默认值。
发布于 2017-03-17 05:28:25
使用彼得的代码(https://stackoverflow.com/a/41618462/7723861),我想出了这个解决方案来获得后置摄像头:
function handleSuccess(stream) {
window.stream = stream; // make stream available to browser console
video.srcObject = stream;
}
function handleError(error) {
console.log('navigator.getUserMedia error: ', error);
}
var DEVICES = [];
var final = null;
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
var arrayLength = devices.length;
for (var i = 0; i < arrayLength; i++)
{
var tempDevice = devices[i];
//FOR EACH DEVICE, PUSH TO DEVICES LIST THOSE OF KIND VIDEOINPUT (cameras)
//AND IF THE CAMERA HAS THE RIGHT FACEMODE ASSING IT TO "final"
if (tempDevice.kind == "videoinput")
{
DEVICES.push(tempDevice);
if(tempDevice.facingMode == "environment" ||tempDevice.label.indexOf("facing back")>=0 )
{final = tempDevice;}
}
}
var totalCameras = DEVICES.length;
//If couldnt find a suitable camera, pick the last one... you can change to what works for you
if(final == null)
{
//console.log("no suitable camera, getting the last one");
final = DEVICES[totalCameras-1];
};
//Set the constraints and call getUserMedia
var constraints = {
audio: false,
video: {
deviceId: {exact: final.deviceId}
}
};
navigator.mediaDevices.getUserMedia(constraints).
then(handleSuccess).catch(handleError);
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});发布于 2017-01-13 00:37:43
通过Cordova将我们的web应用程序部署到android上,我尝试了多种解决方案来访问后置摄像头。对我有效的解决方案是:
constraints = {
audio: false,
video: {
width: 400,
height: 300,
deviceId: deviceId ? {exact: deviceId} : undefined
}
};通过以下方式检索deviceId:
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
// devices is an array of accessible audio and video inputs. deviceId is the property I used to switch cameras
})
.catch(function(err) {
console.log(err.name + ": " + error.message);
});我选择不使用Cordova插件,这样如果我们选择离开Cordova,就不会有如此大规模的迁移。
https://stackoverflow.com/questions/32086122
复制相似问题