请问js如何通过浏览器调用用户摄像头数据流?
网友回复
不要在iframe内打开,必须在https或localhost下才能读取摄像头,代码如下:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <video src=""></video> <script type="text/javascript"> var opt = { audio: true, video: { width: 375, height:260 } }; navigator.mediaDevices.getUserMedia(opt) .then(function(mediaStream) { var video = document.querySelector('video'); video.srcObject = mediaStream; video.onloadedmetadata = function(e) { video.play(); }; }) .catch(function(err) { console.log(err.name + ": " + err.message); }); // always check for errors at the end. </script> </body> </html>