ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript调用摄像头

2021-04-19 09:34:35  阅读:1240  来源: 互联网

标签:调用 facingMode localStream javascript camera video modal 摄像头


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" }

 

标签:调用,facingMode,localStream,javascript,camera,video,modal,摄像头
来源: https://www.cnblogs.com/diehuacanmeng/p/14675516.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有