首页IT科技h5二维码扫码演示(H5项目实现扫描二维码功能 — html5-qrcode)

h5二维码扫码演示(H5项目实现扫描二维码功能 — html5-qrcode)

时间2025-06-18 01:22:40分类IT科技浏览4540
导读:vue3中使用html5-qrcode实现扫描二维码功能。...

vue3中使用html5-qrcode实现扫描二维码功能               。

实现原理:调取手机摄像头扫一扫功能实现

注:调取手机摄像头需要再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>

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
.i文件是什么文件(iwctrl.exe是什么进程 iwctrl进程查询) css图片设置长宽100%,但是与外层盒子偏移(css图片适应盒子大小)