html5-qrcode实现扫码功能——2024-04-19

本文介绍了如何在H5项目中利用html5-qrcode库实现扫描二维码或条形码的功能,包括库的安装、引入,以及如何在HTML中创建扫描容器和处理扫码逻辑,还提到了相关参数配置和错误处理。
摘要由CSDN通过智能技术生成

项目需求:H5项目需要实现扫描二维码或条形码功能。

html5-qrcode使用

1.安装

npm install html5-qrcode --save-dev 

2.引入


import { Html5Qrcode } from 'html5-qrcode';

 3.定义所需变量

  data: function() {
    return {
      html5QrCode: null,
      isShow: false,
      scanReasonList: []
    };
  },

4.创建容器(可根据需求写样式)

<div class="reader" v-if="isShow">
   <div id="reader-box"></div>
</div>

5.实现扫码逻辑

在一个button或者其他节点上绑定事件“scanCode”

    scanCode() {
      this.startScan();
    },   
    startScan() {
      this.isShow = true;
      Html5Qrcode.getCameras().then(devices => {
        if (devices && devices.length) {
          this.html5Qrcode = new Html5Qrcode('reader-box');
          this.html5Qrcode.start(
            {
              facingMode: 'environment'
            },
            {
              fps: 10,
              qrbox: { width: 250, height: 250 }
            },
            (decodeText, decodeResult) => {
              if (decodeText) {
                this.scanReasonList.push(decodeText);
                this.stopScan();
                this.isShow = false;
              }
            },
            err => {
              console.log('失败', err);
            }
          );
        }
      });
    },
    stopScan() {
      this.html5Qrcode.stop();
    },

至此,可基本实现扫码功能。具体的参数配置可参考官网 Getting started | ScanApp

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值