<div> <video id="video" style="width=100%; height=100%; object-fit: fill" autoplay ref="videos" v-show="showVideo"></video> <canvas v-show="!showVideo" width="300px" height="390px" id="personImgSize"></canvas> </div>
一進入這個頁面就開始調用這個函數
在mouted中調用這個函數
autoMakePhoto(){ let canvas = document.getElementsByTagName('canvas')[0]; this.context = canvas.getContext('2d'); let _this=this; if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true, }).then(function (stream) { _this.MediaStreamTrack=typeof stream.stop==='function'?stream:stream.getTracks()[0]; let video = _this.$refs.videos; video["srcObject"]=stream; video.play(); }).catch(function(err){ console.log(err); //調用攝像頭失敗給的提示 }); } },
離開這個頁面 關閉攝像頭###
beforeDestroy(){ this.cancalCloseVideo(); } methods:{ cancalCloseVideo(){ this.MediaStreamTrack && this.MediaStreamTrack.stop(); } }