当前位置: 代码网 > it编程>网页制作>html5 > html5项目实现扫描二维码功能

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

2023年10月07日 html5 我要评论
html5项目实现扫描二维码功能扫描二维码是很常见的一种功能,本文就来介绍一下html5项目实现扫描二维码功能,具有一定的参考价值,感兴趣的可以了解一下... 23-10-07

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

实现

1、下载html5-qrcode

npm i html5-qrcode

2、使用

<template>
    <div class="container" v-show="isshow">
      <div id="reader"></div>
    </div>
</template>
<script setup>
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)
            })
    }
}
</script>
<style lang="scss" scoped>
.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%);
}
</style>

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com