scan.vue 4.06 KB
<!--
 * @Date: 2024-09-15 11:45:13
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-09-15 18:54:36
 * @FilePath: /map-demo/src/views/bieyuan/scan.vue
 * @Description: 文件描述
-->
<template>
  <div class="scan-page">
    <!--<div class="scan_wrapper">
      <div class="scan_box">
        <!~~ 镜头区域 ~~>
        <video ref="video" id="video" class="scan-video" autoplay></video>
      </div>
      <div @click="openScan" class="scan_text">点击扫描二维码查看详情</div>
    </div>
    <div class="sys_logo">
      <van-image
        width="3rem"
        height="3rem"
        fit="contain"
        src="https://cdn.ipadbiz.cn/bieyuan/map/icon/scan_logo.png"
      />
    </div>-->

    <video ref="video" id="video" class="video vjs-fluid" autoplay></video>
    <div v-show="tipShow" class="tip">{{tipMsg}}</div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";

const $route = useRoute();
const $router = useRouter();

import { BrowserMultiFormatReader } from "@zxing/library";
// import axios from 'axios'
const codeReader = ref(null);

const scanText = ref("");
// 初始化相机
const openScan = () => {
  codeReader.value = new BrowserMultiFormatReader();
  codeReader.value
    .getVideoInputDevices()
    .then((videoDevices) => {
      let firstDeviceId = videoDevices[videoDevices.length - 1].deviceId;
      if (videoDevices.length > 1) {
        // 一般通过判断摄像头列表项里的 label 字段,'camera2 0, facing back' 字符串含有 'back' 和 '0',大部分机型是这样,如果有些机型没有,那就还是默认获取最后一个
        firstDeviceId = videoDevices.find((el) => {
          return el.label.indexOf("back") > -1 && el.label.indexOf("0") > -1;
        })
          ? videoDevices.find((el) => {
              return el.label.indexOf("back") > -1 && el.label.indexOf("0") > -1;
            }).deviceId
          : videoDevices[videoDevices.length - 1].deviceId;
      }
      decodeFromInputVideoFunc(firstDeviceId);
    })
    .catch((err) => {
      console.log(err);
    });
};

// 扫码
const decodeFromInputVideoFunc = (firstDeviceId) => {
  // 使用摄像头扫描
  codeReader.value.reset(); // 重置
  codeReader.value.decodeFromInputVideoDeviceContinuously(
    firstDeviceId,
    "video",
    (result, err) => {
      if (result) {
        alert(result.text);
        console.log("扫码结果", result);
        scanText.value = result.text;
        if (scanText.value) {
          // 识别成功关闭摄像头
          codeReader.value.reset();
          codeReader.value.stopContinuousDecodeFromInputVideoDevice();
        }
      }
    }
  );
};

onMounted(() => {});
</script>


<style lang="less" scoped>
.scan-page {
  position: relative;
  height: 100vh;
  .scan_wrapper {
    display: flex;
    padding: 1rem;
    flex-direction: column;
    align-items: center;
    padding-top: 5rem;
    .scan_box {
      padding: 1rem;
      background-image: url('https://cdn.ipadbiz.cn/bieyuan/map/icon/scan_bg.png');
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center center;
      height: 20rem;
      width: 90%;
      position: relative;
      overflow: hidden; /* 确保溢出内容被隐藏 */
      box-sizing: border-box; /* 包括 padding 在内的尺寸计算 */
      .scan-video {
        object-fit: cover; /* 保持视频内容的比例,同时填满容器 */
        position: absolute; /* 确保视频覆盖整个容器 */
        top: -1rem;
        left: -1rem;
        width: calc(100% - 2rem); /* 视频宽度减去左右1rem的padding */
        height: calc(100% - 2rem); /* 视频高度减去上下1rem的padding */
        padding: 2rem;
        object-fit: cover; /* 保持视频比例,同时填充容器 */
      }
    }
    .scan_text {
      font-size: 0.9rem;
      color: #DD7850;
      padding: 0.5rem 1rem;
      margin: 1rem;
      border: 1px solid #DD7850;
      border-radius: 5px;
    }
  }
  .sys_logo {
    position: absolute;
    bottom: 3rem;
    left: calc(50% - 1.5rem);
  }
}
</style>