简体   繁体   English

如何使用后置摄像头代替前置摄像头?

[英]How to use back camera instead of the front camera?

I have a QR scanning camera, but when I open my website on a cell phone, the face camera turns on in my website.我有一个 QR 扫描摄像头,但是当我在手机上打开我的网站时,面部摄像头会在我的网站中打开。 I want to use the back camera.我想用后置摄像头。

function setwebcam()
{

 var constraints = { audio: true, video: true };




document.getElementById("result").innerHTML="- scanning -";
if(stype==1)
{
    setTimeout(captureToCanvas, 500);    
    return;
}
var n=navigator;
document.getElementById("outdiv").innerHTML = vidhtml;
v=document.getElementById("v");

if(n.getUserMedia)
    n.getUserMedia(constraints, success, error);
else
if(n.webkitGetUserMedia)
{
    webkit=true;
    n.webkitGetUserMedia(constraints, success, error);
}
else
if(n.mozGetUserMedia)
{
    moz=true;
    n.mozGetUserMedia(constraints, success, error);
}

If you want to force the device to use front camera you can add facingMode in constraints:如果要强制设备使用前置摄像头,可以在约束中添加facesMode

var constraints = { 
  audio: true,
  video: {
    facingMode: 'environment'
};

or use exact :或使用exact

facingMode: { exact: 'environment' }

You can use MediaStreamTrack.getSources(callback) to get all media sources and their id's.您可以使用MediaStreamTrack.getSources(callback)获取所有媒体源及其 ID。 With other properties you can filter to check is it audio video or filter by name.使用其他属性,您可以过滤以检查它是音频视频还是按名称过滤。 Once you know id of media source you want to show, use it to attach it to video tag.一旦您知道要显示的媒体源的 id,就可以使用它来将其附加到视频标签上。

This is good example:这是一个很好的例子:
https://simpl.info/getusermedia/sources/ https://simpl.info/getusermedia/sources/
https://github.com/samdutton/simpl/blob/master/getusermedia/sources/js/main.js https://github.com/samdutton/simpl/blob/master/getusermedia/sources/js/main.js

If the previous examples did not work for you, try this other way:如果前面的示例不适合您,请尝试其他方式:

Webcam.set({
 width: 250,
 height: 200,
 image_format: 'jpeg',
 jpeg_quality: 90,
 // I add this object constraints
 constraints: {
   facingMode: 'environment'
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM