主要用到的是tracking.js,在组建中引入tracking.js和face-min.js
<video autoplay playsinline ref="video" id="video" class="viedert" v-show="true"></video> <canvasid="canvas"class="canvas"></canvas>
import tracking from‘@/assets/js/tracking-min.js‘
import ‘@/assets/js/face-min.js‘ export default{ components:{ tracking }, data(){ return{ submitBase:"",//存储拍到的base64照片,作为接口参数 videoEle: null, trackerTask: null, mediaStreamTrack: null } }, mounted() { this.formTable() }, methods:{ formTable(){ //flag是一个开关,不然tracking.js会不断的拍照 let flag = true; let tracker = new window.tracking.ObjectTracker("face"); tracker.setStepSize(2); //转头角度影响识别率 tracker.setEdgesDensity(0, 13); tracker.setInitialScale(5); this.trackerTask = window.tracking.track("video", tracker, { camera: true }); tracker.on("track", event => { if (event.data.length) { //有头像有头像执行 //会一直拍照,所有这里加个锁 if (flag) { let vm = this; let video = this.mediaStreamTrack = document.querySelector("video"); let canvas1 = document.querySelector("canvas"); let context1 = canvas1.getContext("2d"); canvas1.setAttribute("width", video.videoWidth); canvas1.setAttribute("height", video.videoHeight); flag = false; let image = context1.drawImage( video, 0, 0, video.videoWidth, video.videoHeight ); let getBase = canvas1.toDataURL("image/png", 1); //1表示质量(无损压缩) this.submitBase = getBase; if (this.submitBase!== ‘‘) { this.$router.push(‘/success‘);
//下面这句话是关闭摄像头的 this.mediaStreamTrack.srcObject.getTracks()[0].stop(); } //下面就是axios调用接口了 }