html5项目实现扫描二维码功能

实现原理:调取手机摄像头扫一扫功能实现
注:调取手机摄像头需要再https协议下才可以,有的项目启动配置https:true可以实现启动的项目协议为https且可以访问,有的还是需要证书才能访问

实现

1、下载html5-qrcode

1
npm i html5-qrcode

2、使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<div class="container">
      <div id="reader"></div>
    </div>
 
import { reactive, onMounted, onUnmounted, ref } from 'vue'
import { showToast  } from 'vant';
import { Html5Qrcode } from 'html5-qrcode'
let html5QrCode = ref(null)
onMounted(() => {
  getCameras()
})
const onSearchOrder = () => {
    console.log('在运运单')
    router.push('order')
}
onUnmounted(() => {
    stop()
})
const getCameras = () => {
    Html5Qrcode.getCameras()
        .then((devices) => {
            if (devices && devices.length) {
                isShow.value = true
                html5QrCode = new Html5Qrcode('reader')
                // start开始扫描
                start()
            }
        })
        .catch((err) => {
            // handle err
            console.log('获取设备信息失败', err) // 获取设备信息失败
            showToast('获取设备信息失败')
        })
}
const start = () => {
    html5QrCode
        .start(
            {facingMode: "environment" },
            {
                fps: 20, // 设置每秒多少帧
                qrbox: { width: 250, height: 250 } // 设置取景范围
                // scannable, rest shaded.
            },
            (decodedText, decodedResult) => {
                alert('扫码结果' + decodedText)
            },
            (errorMessage) => {
                // parse error, ideally ignore it. For example:
                // console.log(`QR Code no longer in front of camera.`);
                console.log('暂无额扫描结果', errorMessage)
            }
        )
        .catch((err) => {
            // Start failed, handle it. For example,
            console.log(`Unable to start scanning, error: ${err}`)
        })
}
const stop = () => {
    if (devicesInfo.value) {
        html5QrCode
            .stop()
            .then((ignore) => {
                // QR Code scanning is stopped.
                console.log('QR Code scanning stopped.', ignore)
            })
            .catch((err) => {
                // Stop failed, handle it.
                console.log('Unable to stop scanning.', err)
            })
    }
}
 
.container {
    position: relative;
    top: 0px;
    left: 0px;
    height: 100vh;
    width: 100%;
    background: rgba($color: #000000, $alpha: 0.48);
    z-index: 999;
}
#reader {
    top: 50%;
    left: 0;
    transform: translateY(-50%);
}

到此这篇关于html5项目实现扫描二维码功能的文章就介绍到这了,更多相关html5扫描二维码内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章,希望大家以后多多支持IT俱乐部! 

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/navsub/html5/9346.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部