|
@@ -1,6 +1,9 @@
|
|
|
<template>
|
|
|
- <div>
|
|
|
+ <div class="videoContainer">
|
|
|
<video ref="video" autoplay muted playsinline></video>
|
|
|
+ <div id="timestamp" v-show="false">
|
|
|
+ {{ datetimeData.date + " " + datetimeData.time }}
|
|
|
+ </div>
|
|
|
<!-- <el-button @click="startCamera">开启摄像头</el-button>
|
|
|
<el-button @click="stopCamera" :disabled="!isCameraActive"
|
|
|
>关闭摄像头</el-button
|
|
@@ -11,6 +14,8 @@
|
|
|
|
|
|
<script>
|
|
|
import { GetPersonByFace, GetPersonByFaceBase64 } from "@/API/custom";
|
|
|
+import Dayjs from "dayjs";
|
|
|
+
|
|
|
export default {
|
|
|
props: {
|
|
|
processStep: {
|
|
@@ -56,9 +61,29 @@ export default {
|
|
|
isCameraActive: false,
|
|
|
stream: null,
|
|
|
captureInterval: null, // 新增定时器变量
|
|
|
+ datetimeData: {
|
|
|
+ date: Dayjs(new Date()).format("YYYY-MM-DD"),
|
|
|
+ time: Dayjs(new Date()).format("HH:mm:ss"),
|
|
|
+ week: Dayjs(new Date()).format("dddd"),
|
|
|
+ },
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
+ refreshTime() {
|
|
|
+ let speed = 1000;
|
|
|
+ let timer = null;
|
|
|
+ let theNowTime = () => {
|
|
|
+ // this.datePersonData.time = Dayjs(new Date()).format('HH:mm:ss')
|
|
|
+ this.datetimeData.time = Dayjs(new Date()).format("HH:mm:ss");
|
|
|
+ this.datetimeData.date = Dayjs(new Date()).format("YYYY-MM-DD");
|
|
|
+ this.datetimeData.week = Dayjs(new Date()).format("dddd");
|
|
|
+ };
|
|
|
+ timer = setInterval(theNowTime, speed);
|
|
|
+ this.$once("hook:beforeDestroy", () => {
|
|
|
+ clearInterval(timer);
|
|
|
+ timer = null;
|
|
|
+ });
|
|
|
+ },
|
|
|
async startCamera() {
|
|
|
try {
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
@@ -143,11 +168,36 @@ export default {
|
|
|
|
|
|
async captureImage() {
|
|
|
const video = this.$refs.video;
|
|
|
+ if (video.paused) return;
|
|
|
const canvas = document.createElement("canvas");
|
|
|
- canvas.width = video.videoWidth;
|
|
|
- canvas.height = video.videoHeight;
|
|
|
- canvas.getContext("2d").drawImage(video, 0, 0);
|
|
|
|
|
|
+ const cropWidth = video.videoWidth * 0.5;
|
|
|
+ const cropHeight = video.videoHeight * 0.98;
|
|
|
+
|
|
|
+ // 设置canvas尺寸为选择区域大小
|
|
|
+ canvas.width = cropWidth;
|
|
|
+ canvas.height = cropHeight;
|
|
|
+
|
|
|
+ const x = (video.videoWidth - cropWidth) / 2;
|
|
|
+ const y = (video.videoHeight - cropHeight) / 2;
|
|
|
+
|
|
|
+ const ctx = canvas.getContext("2d");
|
|
|
+ //中心60%区域
|
|
|
+ ctx.drawImage(
|
|
|
+ video,
|
|
|
+ x,
|
|
|
+ y,
|
|
|
+ cropWidth,
|
|
|
+ cropHeight,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ cropWidth,
|
|
|
+ cropHeight
|
|
|
+ );
|
|
|
+ // canvas.width = video.videoWidth;
|
|
|
+ // canvas.height = video.videoHeight;
|
|
|
+ // const ctx = canvas.getContext("2d");
|
|
|
+ // ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
|
|
try {
|
|
|
const base64Data = canvas.toDataURL("image/png");
|
|
|
// console.log("上传数据:Base64", base64Data);
|
|
@@ -169,11 +219,12 @@ export default {
|
|
|
const result = await response.json();
|
|
|
// console.log("上传反馈", response, result);
|
|
|
if (result.code === 20000) {
|
|
|
- console.log("服务器返回数据:", result);
|
|
|
+ // console.log("服务器返回数据:", result);
|
|
|
this.$emit("output", result.data);
|
|
|
- setTimeout(() => {
|
|
|
- this.stopCamera();
|
|
|
- }, 200);
|
|
|
+ this.uploadImage();
|
|
|
+ // setTimeout(() => {
|
|
|
+ // this.stopCamera();
|
|
|
+ // }, 200);
|
|
|
}
|
|
|
} catch (err) {
|
|
|
console.error("上传过程中出错:", err);
|
|
@@ -182,9 +233,36 @@ export default {
|
|
|
async uploadImage() {
|
|
|
const video = this.$refs.video;
|
|
|
const canvas = document.createElement("canvas");
|
|
|
- canvas.width = video.videoWidth;
|
|
|
- canvas.height = video.videoHeight;
|
|
|
- canvas.getContext("2d").drawImage(video, 0, 0);
|
|
|
+
|
|
|
+ const cropWidth = video.videoWidth * 0.5;
|
|
|
+ const cropHeight = video.videoHeight * 0.98;
|
|
|
+
|
|
|
+ // 设置canvas尺寸为选择区域大小
|
|
|
+ canvas.width = cropWidth;
|
|
|
+ canvas.height = cropHeight;
|
|
|
+
|
|
|
+ const x = (video.videoWidth - cropWidth) / 2;
|
|
|
+ const y = (video.videoHeight - cropHeight) / 2;
|
|
|
+
|
|
|
+ const ctx = canvas.getContext("2d");
|
|
|
+ //中心60%区域
|
|
|
+ ctx.drawImage(
|
|
|
+ video,
|
|
|
+ x,
|
|
|
+ y,
|
|
|
+ cropWidth,
|
|
|
+ cropHeight,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ cropWidth,
|
|
|
+ cropHeight
|
|
|
+ );
|
|
|
+ // ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
|
|
+ const timestampDiv = document.getElementById("timestamp");
|
|
|
+ // 在canvas上绘制时间戳
|
|
|
+ ctx.font = "24px Arial";
|
|
|
+ ctx.fillStyle = "white";
|
|
|
+ ctx.fillText(timestampDiv.textContent, 20, canvas.height - 20);
|
|
|
try {
|
|
|
const base64Data = canvas.toDataURL("image/png");
|
|
|
// console.log("上传数据:Base64", base64Data);
|
|
@@ -201,7 +279,7 @@ export default {
|
|
|
}),
|
|
|
});
|
|
|
const result = await response.json();
|
|
|
- console.log("上传反馈", response, result);
|
|
|
+ // console.log("上传反馈", response, result);
|
|
|
if (result.code === 20000) {
|
|
|
console.log("服务器上传返回数据:", result);
|
|
|
this.$emit("outputPath", result.data);
|
|
@@ -217,6 +295,7 @@ export default {
|
|
|
},
|
|
|
created() {
|
|
|
this.startCamera();
|
|
|
+ this.refreshTime();
|
|
|
},
|
|
|
beforeDestroy() {
|
|
|
this.stopCamera();
|
|
@@ -225,11 +304,28 @@ export default {
|
|
|
</script>
|
|
|
|
|
|
<style scoped>
|
|
|
+.videoContainer {
|
|
|
+ position: relative;
|
|
|
+ width: 50%; /* 容器宽度设为50% */
|
|
|
+ height: 100%; /* 容器高度设为100% */
|
|
|
+ margin: 0 auto; /* 居中显示 */
|
|
|
+ overflow: hidden; /* 隐藏溢出部分 */
|
|
|
+}
|
|
|
video {
|
|
|
height: 800px;
|
|
|
width: 1280px;
|
|
|
+ transform: translate(-25%, 0%);
|
|
|
/* width: 100%;
|
|
|
max-width: 500px; */
|
|
|
background: #000;
|
|
|
}
|
|
|
+#timestamp {
|
|
|
+ position: absolute;
|
|
|
+ bottom: 20px;
|
|
|
+ left: 20px;
|
|
|
+ color: white;
|
|
|
+ font-size: 24px;
|
|
|
+ background-color: rgba(0, 0, 0, 0.5);
|
|
|
+ padding: 5px;
|
|
|
+}
|
|
|
</style>
|