您好,欢迎来到纷纭教育。
搜索
您的当前位置:首页vue2扫描二维码

vue2扫描二维码

来源:纷纭教育

1.安装指定版本vue-qrcode-reader

npm install --save vue-qrcode-reader@3

2.封装扫一扫组件

<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>
</template>
<script>
// 引入
import { QrcodeStream } from "vue-qrcode-reader";

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

  data() {
    return {
      result: "", // 扫码结果信息
      error: "", // 错误信息
      show: false,
      qrcode: true,
      torchActive: false,
      camera: "rear", //front为前置摄像头,rear为后置摄像头
    };
  },
  methods: {
    onDecode(result) {
      this.result = result;
      this.$emit('decode', this.result) //把扫到的额值传递给父组件
    },
    async onInit(promise) {
      try {
        await promise;
      } catch (error) {
        console.log(error,'error')
        if (error.name === "NotAllowedError") {
          this.$message.error("请允许摄像头访问权限");
        } else if (error.name === "NotFoundError") {
          this.$message.error("该设备无摄像头");
        } else if (error.name === "NotSupportedError") {
          this.$message.error("所需的安全上下文(HTTPS、本地主机)");
        } else if (error.name === "NotReadableError") {
          this.$message.error("相机被占用");
        } else if (error.name === "OverconstrainedError") {
          this.$message.error("安装摄像头不合适");
        } else if (error.name === "StreamApiNotSupportedError") {
          this.$message.error("此浏览器不支持流API");
        }
      }
    },
  },
};
</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>

3.页面中引入组件

//html部分
<vue-qrcode @decode="handleUpdateValue"></vue-qrcode>
//js部分
import VueQrcode from '@/components/Qrcode';
components: { VueQrcode },
handleUpdateValue(val) {
  console.log(val) //扫描到的值
},

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务