react项目中使用html5-qrcode

1、安装html5-qrcode

npm install html5-qrcode

2、引入html5-qrcode库

import { Html5QrcodeScanner, Html5Qrcode } from "html5-qrcode";

3、示例写法

let html5Qrcode;
        const getCamera = () => {
      Html5Qrcode.getCameras()
        .then((devices) => {

          if (devices && devices.length) {
              html5Qrcode = new Html5Qrcode("reader");
              start(); //打开相机
          }
        })
        .catch((error) => {
          html5Qrcode = new Html5Qrcode("reader");
          alert("您需要开启相机权限!");
        });
    };
    const start = () => {
      html5Qrcode
        .start(
          {

            facingMode: "environment",//默认使用后置摄像头
          },
          {
            fps: 5,
            qrbox: {//扫码框的大小
              width: 250,
              height: 250,
            },
            aspectRatio: 1.7777778,//显示的比例
          },
          (decodedText, decodeResult) => {
            console.log(decodeResult);
          }
        )
        .catch((error) => {
          alert(error);
        });
    };

    const stop = () => {
        html5Qrcode.stop().then(() => {
         
        });
    };

如果使用默认扫码引入Html5QrcodeScanner

注意:调试时候浏览器必须是在localhost或者是https协议下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值