单应用项目,可以创建很多独立工具类页面 ,不用登录 初始化的页面
gx
chenyc
2025-04-21 0e86a3fc620497b8e5b9de829ddced62bfdf19af
src/views/deviceWindoes2.vue
@@ -760,21 +760,21 @@
          <el-space fill>
            <el-alert type="warning" show-icon :closable="false">
              <p>"请输入设备编号后才能使用不然无法定位到数据来源:</p>
              <p>也可以选择二维码图片识别</p>
            </el-alert>
            <el-form-item label="设备编号">
              <el-input v-model="deviceCode" />
            </el-form-item>
          </el-space>
        </el-form>
        <video ref="video" width="300" height="300" autoplay></video>
        <input type="text" v-model="codeResult" placeholder="扫描结果将会显示在这里">
        <div>
          <input type="file" pattern="选取二维码" accept="image/*" @change="onFileChange"  />
          <input type="text" v-model="codeResult" placeholder="扫描结果将会显示在这里">
        </div>
      </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>
@@ -787,6 +787,7 @@
    
  </template>
  <script lang="ts" setup>
  import { BrowserMultiFormatReader, NotFoundException, ChecksumException, FormatException } from '@zxing/library';
  import TQS88 from "../img/TQS88.png";
  import shezhi from '../img/shezhi.png'
  import xinlv from '../img/xinlv.png'
@@ -798,7 +799,6 @@
  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";
@@ -1186,45 +1186,36 @@
      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 onFileChange=async(event: Event)=> {
    const inputElement = event.target as HTMLInputElement;
    if (!inputElement.files || inputElement.files.length === 0) return;
    // 解析二维码
    const code = jsQR(imageData.data, imageData.width, imageData.height);
    if (code) {
      codeResult.value = code.data;
    }
    const file = inputElement.files[0];
    const reader = new FileReader();
    // 定期捕获帧
    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();
    reader.onload = async (e) => {
      if (e.target && typeof e.target.result === 'string') {
        try {
          const codeReader = new BrowserMultiFormatReader();
          const result = await codeReader.decodeFromImage(undefined, e.target.result);
          codeResult.value = result.text;
        } catch (err) {
          if (err instanceof NotFoundException) {
            console.error("未找到二维码");
          } else if (err instanceof ChecksumException) {
            console.error("校验错误");
          } else if (err instanceof FormatException) {
            console.error("格式错误");
          } else {
            console.error(err);
          }
        }
      }
    } catch (e) {
      console.error("无法访问摄像头", e);
    }
    };
    reader.readAsDataURL(file);
  }
}
  const shaoma=()=>{
    startCamera()
  }
  watch(
    () => deviceData.value.设备变化,