| | |
| | | <template> |
| | | <teleport to="body"> |
| | | <div class="mask"> |
| | | <el-header class="go"> |
| | | <div class="box"> |
| | | <video id="videoCamera" class="canvas" :width="videoWidth" :height="videoHeight" autoPlay></video> |
| | | <canvas id="canvasCamera" class="canvas" :width="videoWidth" :height="videoWidth"></canvas> |
| | | </div> |
| | | <div class="footer"> |
| | | <el-button @click="getCompetence" icon="el-icon-video-camera"> 打开摄像头 </el-button> |
| | | <el-button @click="drawImage" icon="el-icon-camera"> 拍照 </el-button> |
| | | <el-button @click="stopNavigator" icon="el-icon-switch-button"> 关闭摄像头 </el-button> |
| | | <el-button @click="resetCanvas" icon="el-icon-refresh"> 重置 </el-button> |
| | | <el-button @click="$emit('closed')" class="closeBtn">关闭</el-button> |
| | | </div> |
| | | </el-header> |
| | | </div> |
| | | </teleport> |
| | | </template> |
| | | <script lang="ts" setup> |
| | | import { ref, reactive, inject, toRefs, nextTick } from "vue"; |
| | | import { ElMessage, ElMessageBox } from "element-plus"; |
| | | const message = inject("$message"); |
| | | const config = inject("$CONFIG"); |
| | | const tool = inject("$TOOL"); |
| | | const api = inject("$API"); |
| | | <script setup lang="ts"> |
| | | import { computed, onMounted, ref } from "vue"; |
| | | import {base64toFile} from '@/samples/faceApi' |
| | | import { formatDate } from "@/utils/formatTime"; |
| | | import { confingInfoStore } from "@/stores/StoresConfing"; |
| | | |
| | | const loading = ref(false); |
| | | const os = ref(false); //控制摄像头开关 |
| | | let thisVideo = ref(""); |
| | | let thisContext = ref(""); |
| | | let thisCancas = ref(""); |
| | | const videoWidth = ref(500); |
| | | const videoHeight = ref(500); |
| | | const postOptions = ref([]); |
| | | const certCtl = ref(""); |
| | | const mask = ref(true); |
| | | |
| | | //查询参数 |
| | | const queryParams = reactive({ |
| | | pageNum: 1, |
| | | pageSize: 10, |
| | | imgSrc: undefined, |
| | | }); |
| | | const closedPhono = ref(null); |
| | | |
| | | const emit = defineEmits(["closed"]); |
| | | const props = defineProps({ |
| | | visible: { type: Boolean }, |
| | | }); |
| | | const { visible } = toRefs(props); |
| | | const handleChange = (val) => { |
| | | console.log(visible); |
| | | }; |
| | | |
| | | //调用摄像头权限 |
| | | const getCompetence = () => { |
| | | nextTick(() => { |
| | | os.value = false; |
| | | thisCancas = document.getElementById("canvasCamera"); |
| | | thisContext = thisCancas.getContext("2d"); |
| | | thisVideo = document.getElementById("videoCamera"); |
| | | closedPhono.value = thisVideo; |
| | | if (navigator.mediaDevices === undefined) { |
| | | navigator.mediaDevices = {}; |
| | | } |
| | | |
| | | if (navigator.mediaDevices.getUserMedia === undefined) { |
| | | navigator.mediaDevices.getUserMedia = function (constraints) { |
| | | // 首先获取现存的getUserMedia(如果存在) |
| | | let getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia; |
| | | if (!getUserMedia) { |
| | | return Promise.reject(new Error("getUserMedia is not implemented in this browser")); |
| | | } |
| | | return new Promise(function (resolve, reject) { |
| | | getUserMedia.call(navigator, constraints, resolve, reject); |
| | | }); |
| | | }; |
| | | } |
| | | |
| | | const constraints = { |
| | | audio: false, |
| | | video: { width: videoWidth.value, height: videoHeight.value, transform: "scaleX(-1)" }, |
| | | }; |
| | | |
| | | navigator.mediaDevices |
| | | .getUserMedia(constraints) |
| | | .then(function (stream) { |
| | | if ("srcObject" in thisVideo) { |
| | | thisVideo.srcObject = stream; |
| | | } else { |
| | | thisVideo.src = window.URL.createObjectURL(stream); |
| | | } |
| | | thisVideo.onloadedmetadata = function (e) { |
| | | thisVideo.play(); |
| | | }; |
| | | }) |
| | | .catch((err) => { |
| | | ElMessage.error("没有开启摄像头权限或浏览器版本不兼容"); |
| | | }); |
| | | }); |
| | | }; |
| | | |
| | | //绘制图片 |
| | | const drawImage = () => { |
| | | getCompetence(); |
| | | thisCancas = document.getElementById("canvasCamera"); |
| | | thisContext = thisCancas.getContext("2d"); |
| | | thisVideo = document.getElementById("videoCamera"); |
| | | thisContext.drawImage(thisVideo, 0, 0, 500, 500); |
| | | //获取图片地址 |
| | | queryParams.imgSrc = thisCancas.toDataURL('image/png'); |
| | | console.log(queryParams.imgSrc); |
| | | }; |
| | | |
| | | //清空画布 |
| | | const clearCanvas = (id) => { |
| | | let c = document.getElementById(id); |
| | | let cxt = c.getContext("2d"); |
| | | cxt.clearRect(0, 0, 500, 500); |
| | | |
| | | }; |
| | | |
| | | //重置画布 |
| | | const resetCanvas = () => { |
| | | queryParams.imgSrc = ""; |
| | | clearCanvas("canvasCamera"); |
| | | }; |
| | | |
| | | //关闭摄像头 |
| | | const stopNavigator = () => { |
| | | // thisVideo = document.getElementById("videoCamera"); |
| | | if (closedPhono.value && closedPhono.value !== null) { |
| | | thisVideo.srcObject.getTracks()[0].stop(); |
| | | os.value = true; |
| | | } else { |
| | | ElMessage.error("没有开启摄像头权限或浏览器版本不兼容"); |
| | | const msg = ref<string>("没识别到人脸..."); |
| | | const dialogVisible=ref(true) |
| | | const date=ref('') |
| | | let trackerTask: any = null; |
| | | const inputCode=ref('') |
| | | const inputRef = ref() |
| | | // 标识用的画布 |
| | | const myCanvas = ref<HTMLCanvasElement | null>(null); |
| | | let imgSrc:''; |
| | | // 实例人脸检查器 |
| | | const myTracker: any = new tracking.ObjectTracker("face"); |
| | | myTracker.setInitialScale(4); |
| | | myTracker.setStepSize(2); |
| | | myTracker.setEdgesDensity(0.1); |
| | | // 监听人脸检查器 |
| | | myTracker.on("track", (event: tracking.TrackEvent) => { |
| | | const context = myCanvas.value?.getContext("2d") as CanvasRenderingContext2D; |
| | | if (myCanvas.value) { |
| | | context.clearRect(0, 0, myCanvas.value.width, myCanvas.value.height); |
| | | } |
| | | if (event.data.length === 0) { |
| | | msg.value = "没识别到人脸..."; |
| | | } else if(event.data.length === 1) { |
| | | trackerTask.stop(); |
| | | msg.value = "检测到人脸"; |
| | | const myCanvas = document.getElementById("myCanvas");// |
| | | const thisContext = myCanvas?.getContext("2d"); |
| | | const myVideo = document.querySelector("#myVideo") as HTMLVideoElement; |
| | | thisContext.drawImage(myVideo, 0,0, 250, 200); |
| | | imgSrc = myCanvas?.toDataURL('image/png'); |
| | | // 转文件 |
| | | // 识别框显示才能到传阿里云识别 |
| | | if(dialogVisible.value){ |
| | | base64toFile(imgSrc) |
| | | } |
| | | }; |
| | | defineExpose({ |
| | | stopNavigator, |
| | | }); |
| | | </script> |
| | | <style scoped> |
| | | .footer { |
| | | width: 959px; |
| | | height: 50px; |
| | | background-color: white; |
| | | justify-content: space-between; |
| | | float: left; |
| | | margin-top: 217px; |
| | | setTimeout(() => { |
| | | console.log('监测到人脸后3s') |
| | | trackerTask.run(); |
| | | }, 3000); |
| | | // @ts-ignore |
| | | if (typeof window.stream === "object") { |
| | | myVideo.srcObject = null; |
| | | // @ts-ignore |
| | | window.stream.getTracks().forEach((track) => track.stop()); |
| | | } |
| | | } |
| | | .detailed { |
| | | width: 1200px !important; |
| | | height: 800px !important; |
| | | display: block; |
| | | position: absolute; |
| | | left: 50%; |
| | | top: 50%; |
| | | transform: translate(-50%, -50%); |
| | | background-color: white; |
| | | } |
| | | }); |
| | | onMounted(() => { |
| | | // 触发人脸检查器 |
| | | console.log('人脸识别初始化') |
| | | const isUseFaceRecogService = confingInfoStore().confingInfo.isUseFaceRecogService |
| | | console.log('人脸识别',isUseFaceRecogService) |
| | | setInterval(function () { |
| | | date.value= formatDate(new Date(),'YYYY-mm-dd HH:MM') |
| | | }, 1000) |
| | | if (isUseFaceRecogService) { |
| | | console.log('开启人脸识别初始化') |
| | | setTimeout(()=>{ |
| | | trackerTask = tracking.track("#myVideo", myTracker, { camera: true }); |
| | | },2000) |
| | | |
| | | |
| | | .oText { |
| | | margin-left: 42px; |
| | | }else{ |
| | | console.log('关闭人脸识别') |
| | | |
| | | } |
| | | .mask { |
| | | position: absolute; |
| | | top: 0; |
| | | left: 0; |
| | | bottom: 0; |
| | | right: 0; |
| | | background-color: rgba(0, 0, 0, 0.5); |
| | | z-index: 999; |
| | | } |
| | | .closeBtn { |
| | | float: right; |
| | | } |
| | | .box { |
| | | width: 959px; |
| | | height: 700px; |
| | | margin-top: 10px; |
| | | background-color: rgba(0, 0, 0, 0.5); |
| | | /* float: left !important; */ |
| | | padding-top: 100px; |
| | | text-align: center; |
| | | } |
| | | .go { |
| | | width: 1000px; |
| | | height: 800px; |
| | | background-color: white; |
| | | position: absolute; |
| | | left: 50%; |
| | | top: 50%; |
| | | transform: translate(-50%, -50%); |
| | | display: inline-block; |
| | | } |
| | | |
| | | </style> |
| | | |
| | | |
| | | }); |
| | | </script> |