Javascript 三星默认浏览器或android中的任何默认浏览器都不支持后置摄像头

Javascript 三星默认浏览器或android中的任何默认浏览器都不支持后置摄像头,javascript,android,browser,Javascript,Android,Browser,下面是我的代码,我正在使用下面的插件制作二维码扫描仪(用于移动设备) 它在移动设备上几乎支持Chrome、Mozilla FireFox、Opera和MS EDGE,但我正试图在所有最新设备上实现默认浏览器(安卓-主要是三星设备)也能正常工作,但不是使用后置摄像头(后置摄像头)而是使用前置摄像头(面向用户)为此,我已经按照Mozilla开发者组编写了一段代码 { facingMode: { exact: "environment" } } 这是我的密码 navigator.mediaDev

下面是我的代码,我正在使用下面的插件制作二维码扫描仪(用于移动设备)

它在移动设备上几乎支持Chrome、Mozilla FireFox、Opera和MS EDGE,但我正试图在所有最新设备上实现默认浏览器(安卓-主要是三星设备)也能正常工作,但不是使用后置摄像头(后置摄像头)而是使用前置摄像头(面向用户)为此,我已经按照Mozilla开发者组编写了一段代码

{ facingMode: { exact: "environment" } }
这是我的密码

 navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: "environment" } } }).then(function(stream) {
                  video.srcObject = stream;
                  video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen
                  video.play();
                  requestAnimationFrame(tick);
                  localStream = stream;
                })
                .catch(function(err) {
                    console.log(err);
                      /* handle the error */
                });

您可以使用facingMode分别为前后摄像头选择“用户”或“环境”

使用

如上所述,您已经在使用,通过删除
exact
,尝试进行如下调用:

navigator.getUserMedia({video: { facingMode: "environment" } },
successCallback, errorCallback);

最后我得到了问题的解决方案,您需要使用navigator.mediaDevices.enumerateDevices并获取设备id并将其分配给getUserMedia,但可能会出现浏览器兼容性问题,我也使用一个条件编写了一个代码

navigator.mediaDevices.enumerateDevices()
                .then(function(devices) {

                var videoDevices = [0,0];
                var videoDeviceIndex = 0;
                devices.forEach(function(device) {
                                        if(device.kind == "videoinput") { 
                                            videoDevices[videoDeviceIndex++] =  device.deviceId;  
                                        }
                 });

                var isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);
                var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
                            if(navigator.userAgent.indexOf("Firefox") !== -1){
                                console.log("Firefox")
                                var constraints =  {facingMode: videoDevices[1] };
                            } else if(isSafari && iOS){
                                console.log("Safari")
                                var constraints =  { facingMode: { exact: "environment" } };
                            } else {
                                console.log("other browser")
                                var constraints =  {deviceId: {  exact: videoDevices[1]  }  }
                            }
                                return navigator.mediaDevices.getUserMedia({ video: constraints });

                              }).then(function(stream) {
                                  video.srcObject = stream;
                                  video.setAttribute("playsinline", true); 
                                  video.play();

                                })
                                .catch(function(err) {
                                   //use this to display the error 
                                    console.log(err);
                                });
应该这样做(第18行):


ps:验证了三星S4仍然存在问题

我已经尝试了这两种代码,但仍然在默认浏览器中使用前置摄像头。。。因此,我正在寻找一个可以强制只选择后置摄像头(后置摄像头)的代码@KundanSIngh您是否只在三星设备上面临这个问题?您是否在Nexus设备上进行过验证?什么是操作系统版本?安卓操作系统版本从4.3到8.1,我还没有登录Nexus!三星Galaxy S4和Nexus 5仍然存在此问题。
navigator.mediaDevices.enumerateDevices()
                .then(function(devices) {

                var videoDevices = [0,0];
                var videoDeviceIndex = 0;
                devices.forEach(function(device) {
                                        if(device.kind == "videoinput") { 
                                            videoDevices[videoDeviceIndex++] =  device.deviceId;  
                                        }
                 });

                var isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);
                var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
                            if(navigator.userAgent.indexOf("Firefox") !== -1){
                                console.log("Firefox")
                                var constraints =  {facingMode: videoDevices[1] };
                            } else if(isSafari && iOS){
                                console.log("Safari")
                                var constraints =  { facingMode: { exact: "environment" } };
                            } else {
                                console.log("other browser")
                                var constraints =  {deviceId: {  exact: videoDevices[1]  }  }
                            }
                                return navigator.mediaDevices.getUserMedia({ video: constraints });

                              }).then(function(stream) {
                                  video.srcObject = stream;
                                  video.setAttribute("playsinline", true); 
                                  video.play();

                                })
                                .catch(function(err) {
                                   //use this to display the error 
                                    console.log(err);
                                });
var constraints =  {facingMode: videoDevices[1] };
var constraints =  {deviceID: {exact: videoDevices[1]} };