From c587f3499ca5eebf81374cf77ed1e0694f8c236b Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期一, 21 四月 2025 15:13:05 +0800
Subject: [PATCH] gx

---
 src/views/deviceWindoes2.vue |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/src/views/deviceWindoes2.vue b/src/views/deviceWindoes2.vue
index 8ba1ec3..1ed4985 100644
--- a/src/views/deviceWindoes2.vue
+++ b/src/views/deviceWindoes2.vue
@@ -766,10 +766,15 @@
             </el-form-item>
           </el-space>
         </el-form>
+        <video ref="video" width="300" height="300" autoplay></video>
+        <input type="text" v-model="codeResult" placeholder="扫描结果将会显示在这里">
       </span>
       <template #footer>
         <div class="dialog-footer">
           <el-button @click="centerDialogVisible = false">取消</el-button>
+          <el-button type="primary" @click="shaoma">
+            扫码录入
+          </el-button>
           <el-button type="primary" @click="saveSet">
             确认
           </el-button>
@@ -793,6 +798,7 @@
   import cljd from '../img/cljd.png'
   import tizhong from '../img/tizhong.png'
   import {EventSourcePolyfill} from 'event-source-polyfill';
+  import jsQR from 'jsqr';
   import { computed, getCurrentInstance, onBeforeMount, onMounted, ref, watch, watchEffect } from "vue";
   import { Local } from '../utils/storage';
   import * as echarts from "echarts";
@@ -983,8 +989,12 @@
     ],
   });
   // 告警提示
-  const textbaojing = ref("");
-  const iscomfig = ref(false);
+  // 识别窗口
+  const video = ref<HTMLVideoElement | null>(null);
+  // 识别文本
+  const codeResult = ref<string>('');
+  // 识别数据流
+  let stream: MediaStream | null = null;
   const centerDialogVisible = ref(false);
   const background = ref("");
   const txztText = ref("");
@@ -1176,6 +1186,46 @@
       ElMessage.warning('请先输入设备编号')
     }
   }
+  function captureFrame() {
+    if (!video.value) return;
+    
+    const canvas = document.createElement('canvas');
+    const context = canvas.getContext('2d');
+    if (!context || !video.value) return;
+    
+    canvas.width = video.value.videoWidth;
+    canvas.height = video.value.videoHeight;
+    context.drawImage(video.value, 0, 0, canvas.width, canvas.height);
+    const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
+
+    // 解析二维码
+    const code = jsQR(imageData.data, imageData.width, imageData.height);
+    if (code) {
+      codeResult.value = code.data;
+    }
+
+    // 定期捕获帧
+    setTimeout(captureFrame, 500);
+  }
+  const  startCamera=async()=> {
+  if (video.value) {
+    try {
+      stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } });
+      if (video.value) {
+        video.value.srcObject = stream;
+        video.value.play();
+
+        // 开始捕获帧
+        captureFrame();
+      }
+    } catch (e) {
+      console.error("无法访问摄像头", e);
+    }
+  }
+}
+  const shaoma=()=>{
+    startCamera()
+  }
   watch(
     () => deviceData.value.设备变化,
     () => {

--
Gitblit v1.8.0