Mik*_*ail 12 javascript getusermedia
我目前正在使用Android平板电脑和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.");
});
Run Code Online (Sandbox Code Playgroud)
我在"强制"部分插入了facingMode,但没有工作.
请帮忙.
jib*_*jib 17
更新: facingMode现在可通过adapter.js polyfill在Chrome for Android中使用!
facingMode是不是在Chrome尚未实施的Android,但在Firefox原生支持的Android.
但是,您必须使用标准约束:( 对Chrome 使用https小提琴):
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>";Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
该{ exact: }语法意味着需要进行约束,如果用户没有正确的相机事情失败.如果你把它留下来,那么约束是可选的,在Firefox for Android中意味着它只会在权限提示中更改摄像机选择器中的默认值.
小智 6
使用Peter的代码(/sf/answers/2913292371/)我想出了这个解决方案来获得后置摄像头:
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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22012 次 |
| 最近记录: |