vue2 扫码 vue-qrcode-reader

2023-08-17  本文已影响0人  缓慢的蜗牛

vue-qrcode-reader 扫一扫二维码插件

此插件必须访问 https 才可访问摄像头

1、安装插件

因是 vue2 版本所以根据官网提示
https://github.com/gruhn/vue-qrcode-reader

How to make it work with Vue 2?

Support is dropped but you can downgrade to vue-qrcode-reader v3.* or lower.

# 为以下安装
npm install --save vue-qrcode-reader@3

2、创建组件

  1. 在 src 下面的 components 创建 qrcode.vue
<template>
  <div>
    <div class="ku-scanner">
      <qrcode-stream
        v-show="qrcode"
        :camera="camera"
        :torch="torchActive"
        @decode="onDecode"
        @init="onInit"
      >
        <div class="ku-scanner-content">
          <div class="ku-scanner-tooltip">
            将二维码/条码放入框内,即自动扫描
          </div>
          <div class="ku-scanner-section">
            <div class="ku-scanner-section-animation-line"></div>
            <div class="ku-scanner-section-angle"></div>
          </div>
        </div>
      </qrcode-stream>
    </div>
    <div class="ku-scanner-error">错误信息:{{ error }}</div>
    <div class="ku-scanner-decode-result">扫描结果:{{ result }}</div>
    <div>
      <van-button type="primary" @click="openCamera">打开相机</van-button>
      <van-button type="info" @click="closeCamera">关闭相机</van-button>
      <van-button type="default" @click="openFlash">打开手电筒</van-button>
      <van-button type="warning" @click="switchCamera">相机反转</van-button>
    </div>
  </div>
</template>
<script>
// 引入
import { QrcodeStream } from "vue-qrcode-reader";

export default {
  // 注册
  components: { QrcodeStream },

  data() {
    return {
      result: "", // 扫码结果信息
      error: "", // 错误信息
      show: false,
      qrcode: true,
      torchActive: false,
      camera: "front",
    };
  },

  methods: {
    onDecode(result) {
      console.log(result);
      this.result = result;
    },
    async onInit(promise) {
      const { capabilities } = await promise;

      const TORCH_IS_SUPPORTED = !!capabilities.torch;
      try {
        await promise;
      } catch (error) {
        if (error.name === "NotAllowedError") {
          this.error = "ERROR: 您需要授予相机访问权限";
        } else if (error.name === "NotFoundError") {
          this.error = "ERROR: 这个设备上没有摄像头";
        } else if (error.name === "NotSupportedError") {
          this.error = "ERROR: 所需的安全上下文(HTTPS、本地主机)";
        } else if (error.name === "NotReadableError") {
          this.error = "ERROR: 相机被占用";
        } else if (error.name === "OverconstrainedError") {
          this.error = "ERROR: 安装摄像头不合适";
        } else if (error.name === "StreamApiNotSupportedError") {
          this.error = "ERROR: 此浏览器不支持流API";
        }
      }
    },
    // 打开相机
    openCamera() {
      this.camera = "rear";
      this.qrcode = true;
      this.show = true;
    },
    // 关闭相机
    closeCamera() {
      this.camera = "off";
      this.qrcode = false;
      this.show = false;
    },
    // 打开手电筒
    openFlash() {
      switch (this.torchActive) {
        case true:
          this.torchActive = false;
          break;
        case false:
          this.torchActive = true;
          break;
      }
    },
    // 相机反转
    switchCamera() {
      // console.log(this.camera);
      switch (this.camera) {
        case "front":
          this.camera = "rear";
          console.log(this.camera);
          break;
        case "rear":
          this.camera = "front";
          console.log(this.camera);
          break;
      }
    },
  },
};
</script>

<style scoped lang="scss">
.ku-scanner-error {
  font-weight: bold;
  font-size: 14px;
  color: red;
}
.ku-scanner-decode-result {
  font-size: 14px;
  color: #000;
}
.ku-scanner {
  height: 282px;
  .ku-scanner-content {
    background-image: linear-gradient(
        0deg,
        transparent 24%,
        rgba(32, 255, 77, 0.1) 25%,
        rgba(32, 255, 77, 0.1) 26%,
        transparent 27%,
        transparent 74%,
        rgba(32, 255, 77, 0.1) 75%,
        rgba(32, 255, 77, 0.1) 76%,
        transparent 77%,
        transparent
      ),
      linear-gradient(
        90deg,
        transparent 24%,
        rgba(32, 255, 77, 0.1) 25%,
        rgba(32, 255, 77, 0.1) 26%,
        transparent 27%,
        transparent 74%,
        rgba(32, 255, 77, 0.1) 75%,
        rgba(32, 255, 77, 0.1) 76%,
        transparent 77%,
        transparent
      );
    background-size: 3rem 3rem;
    background-position: -1rem -1rem;
    width: 100%;
    height: 281px;
    position: relative;
    background-color: rgba(18, 18, 18, 0);
    // 提示信息
    .ku-scanner-tooltip {
      width: 100%;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      text-align: center; /* color: #f9f9f9; */
      margin: 0 auto;
      position: absolute;
      top: 0;
      left: 0;
      color: #ffffff;
    }
    .ku-scanner-section {
      width: 213px;
      height: 213px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      overflow: hidden;
      border: 0.1rem solid rgba(0, 255, 51, 0.2);
      // 装饰角
      &:after {
        content: "";
        display: block;
        position: absolute;
        width: 12px;
        height: 12px;
        border: 0.2rem solid transparent;
        top: 0;
        border-top-color: #00ff33;
        right: 0;
        border-right-color: #00ff33;
      }
      &:before {
        content: "";
        display: block;
        position: absolute;
        width: 12px;
        height: 12px;
        border: 0.2rem solid transparent;
        top: 0;
        border-top-color: #00ff33;
        left: 0;
        border-left-color: #00ff33;
      }
      // 装饰角
      .ku-scanner-section-angle {
        &:after {
          content: "";
          display: block;
          position: absolute;
          width: 12px;
          height: 12px;
          border: 0.2rem solid transparent;
          bottom: 0;
          border-bottom-color: #00ff33;
          right: 0;
          border-right-color: #00ff33;
        }
        &:before {
          content: "";
          display: block;
          position: absolute;
          width: 12px;
          height: 12px;
          border: 0.2rem solid transparent;
          bottom: 0;
          border-bottom-color: #00ff33;
          left: 0;
          border-left-color: #00ff33;
        }
      }
      // 扫描活动线
      .ku-scanner-section-animation-line {
        height: calc(100% - 2px);
        width: 100%;
        background: linear-gradient(
          180deg,
          rgba(0, 255, 51, 0) 43%,
          #00ff33 211%
        );
        border-bottom: 3px solid #00ff33;
        transform: translateY(-100%);
        animation: radar-beam 2s infinite alternate;
        animation-timing-function: cubic-bezier(0.53, 0, 0.43, 0.99);
        animation-delay: 1.4s;
      }
    }
  }
}
// 扫描活动线--上下 动画
@keyframes radar-beam {
  0% {
    transform: translateY(-100%);
  }

  100% {
    transform: translateY(0);
  }
}
</style>

  1. 解决 vue 项目本地访问 https

安装

npm install -D @vitejs/plugin-basic-ssl
# or
# yarn add -D @vitejs/plugin-basic-ssl
# or
# pnpm add -D @vitejs/plugin-basic-ssl

在 vite.config.js 或者 vite.config.ts 中配置:

import basicSsl from '@vitejs/plugin-basic-ssl'  // 引入
server: {
        // ...
        proxy: {
            // ...
        },
        https: true, // 安装basicSsl后,这里可设置可不设置
   },
   plugins: [
        // ...
        basicSsl(),
   ]

重启服务

npm run dev
# or
# yarn dev
# or
# pnpm run dev

启动后可访问 https

  ➜  Local:   https://localhost:8001/
  ➜  Network: https://10.100.251.23:8001/
  ➜  Network: https://192.168.3.87:8001/
上一篇下一篇

猜你喜欢

热点阅读