uni-app的h5扫码(二维码、条形码)

安装@zxing/library

yarn add @zxing/library 或npm i @zxing/library --save

代码

<script>
import { BrowserQRCodeReader } from '@zxing/library'

export default {
  data() {
    return {
      codeReader: new BrowserQRCodeReader()
    }
  },
  async onLoad(options) {
    await this.fetchData()
  },
  destroyed() {
    this.removeDom()
  },
  methods: {
    async fetchData() {
      // 先删除已存在的dom
      this.removeDom()
      // 再创建
      this.createDom()
      await this.openCameraAndScan()
    },
    // 打开摄像头
    async openCameraAndScan() {
      try {
        await this.newScan()
      } catch (err) {
        console.log(err)
        if (err.code && err.code == 404) {
          uni.showToast({
            title: '浏览器不支持,请更换浏览器',
            duration: 2000,
            icon: 'error'
          })
        } else {
          uni.showToast({
            title: '请检查是否存在摄像头',
            duration: 2000,
            icon: 'error'
          })
        }
      }
    },
    async newScan() {
      // 构建回调函数
      const handleQrCode = (result, err) => {
        if (result && result.text) {
          // 判断是否其他网点
         console.log("扫码出的结果",result.text)
        }
        if (err && !err) {
          this.removeDom()
          uni.showToast({
            title: '摄像头扫码失败',
            duration: 2000,
            icon: 'error'
          })
        }
      }
      let result, err
      try {
        console.log('初始化zxing')
        let videoDom = document.getElementById('video')
        result = await this.codeReader.decodeOnceFromVideoDevice(undefined, videoDom)
        console.log('扫码结果', result)
      } catch (ex) {
        console.error('zxing识别错误', ex)
        err = ex
      }
      // 调用回调函数
      handleQrCode(result, err)
    },
    createDom() {
      // 手动创建html video
      // 否则,如果是template中的video,会被转译成univideo
      // 导致zxing无法识别
      let videoBoxDom = document.getElementById('videoBox')
      let videoDom = document.createElement('video')
      videoDom.id = 'video'
      videoDom.style.position = 'absolute'
      // videoDom.style.zIndex = '-2'
      videoDom.style.width = '100%'
      videoDom.style.height = '100%'
      videoDom.style.objectFit = 'cover'
      videoDom.setAttribute('autoplay', true)
      videoDom.setAttribute('muted', true)
      videoBoxDom.appendChild(videoDom)
    },
    removeDom() {
      if (this.codeReader && this.codeReader.reset) {
        this.codeReader.reset()
      }
      if (this.codeReader && this.codeReader.reader && this.codeReader.reader.reset) {
        this.codeReader.reader.reset()
      }
      let videoBoxDom = document.getElementById('videoBox')
      let videoDom = document.getElementById('video')
      if (videoBoxDom && videoDom) {
        videoBoxDom.removeChild(videoDom)
      }
    }
  }
}
</script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值