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