H5扫描二维码获取内容

使用html5-qrcode实现 github:https://github.com/mebjas/html5-qrcode

电脑手机都可以


    <div id="reader"></div>
    let html5QrCode = new Html5Qrcode("reader");    
    let reader = document.getElementById("reader");
    let config = { fps: 10, qrbox: { width: 300, height: 280 } }; //扫一扫相关设置
    //相机授权
    function useCamera() {
        reader.style.display = "block";
        Html5Qrcode.getCameras()
            .then((devices) => {
                console.log(devices, '判断')
                if (devices && devices.length) {
                    let cameraId = "";
                    if (devices.length == 1) {
                        cameraId = devices[0].id; //前置摄像头
                    } else {
                        cameraId = devices[1].id;  //后置摄像头
                    }
                    if (cameraId) {
                        startWithCameraId(cameraId);
                    }
                }
            })
            .catch((err) => {
                console.log("没有获取摄像头设备...");
            });
    }

    //相机
    function startWithCameraId(cameraId) {
        html5QrCode
            .start(
                { deviceId: { exact: cameraId } },
                config,
                onScanSuccess,
                onScanFailure
            )
            .catch((err) => {
                console.log("通过摄像头扫码异常....", err);
            });
    }

    //扫码解析成功
    function onScanSuccess(decodedText, decodedResult) {
        //暂停扫码
        html5QrCode.stop().then((ignore) => {
            $("#device_no").val(decodedText)
            verifyDevice()
            html5QrCode.clear()   //清除画布
            // console.log("扫码成功结果:\n" + decodedText)
        }).catch((err) => {
            console.log(err)
        });

    }

    //扫码解析失败
    function onScanFailure(error) {
        console.log("扫码失败:\n" + error)
    }

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值