本文實(shí)例為大家分享了js調(diào)用設(shè)備攝像頭的具體代碼,供大家參考,具體內(nèi)容如下
使用getUserMedia這個(gè)API來(lái)獲取攝像頭的權(quán)限
兼容chrome和火狐,IOS不兼容
下面是源碼:
<!DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <video src=""></video> <script type="text/javascript"> var opt = { audio: true, video: { width: 375, height: 603 } }; 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>注意,如果使用chrome查看代碼需要在https協(xié)議下才能生效,建議使用火狐查看。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選