js调用设备摄像头的方法
本文实例为大家分享了js调用设备摄像头的具体代码,供大家参考,具体内容如下
使用getUserMedia这个API来获取摄像头的权限
兼容chrome和火狐,IOS不兼容
下面是源码:
Document varopt={ audio:true, video:{ width:375, height:603 } }; navigator.mediaDevices.getUserMedia(opt) .then(function(mediaStream){ varvideo=document.querySelector('video'); video.srcObject=mediaStream; video.onloadedmetadata=function(e){ video.play(); }; }) .catch(function(err){ console.log(err.name+":"+err.message); });//alwayscheckforerrorsattheend.