javascript调用摄像头

const facecamera = { video: { width: 1600, height: 1200, facingMode: "user" } }; // 面部摄像头
const backcamera = { video: { width: 1600, height: 1200, facingMode: "environment" } }; // 背部摄像头
if( modal_camera.localStream == null ) try { modal_camera.localStream = navigator.mediaDevices.getUserMedia(backcamera); } catch (e) { }
if( modal_camera.localStream == null ) try { modal_camera.localStream = navigator.mediaDevices.getUserMedia(facecamera); } catch (e) { }
if( modal_camera.localStream != null ){
    modal_camera.localStream.then(function(stream){
        console.log(stream);
        modal_camera.video.srcObject = stream;
        modal_camera.video.play();
    }).catch(function(error){
        console.log(error.name);
    });
}

在调用摄像头时,需要注意facingMode属性的写法,当前方式可以支持windows平板电脑摄像头的调用,如果写成以下方式,则有可能会发生错误。

facingMode: { exact: "environment" }

 

上一篇:unity在同屏幕显示多Camera并在脚本中修改Viewport Rece


下一篇:2021.4.3| ROS下相机标定