html5項(xiàng)目實(shí)現(xiàn)掃描二維碼功能
發(fā)布時(shí)間:2023-10-07 16:25:11 作者:浮橋
我要評(píng)論

掃描二維碼是很常見(jiàn)的一種功能,本文就來(lái)介紹一下html5項(xiàng)目實(shí)現(xiàn)掃描二維碼功能,具有一定的參考價(jià)值,感興趣的可以了解一下
實(shí)現(xiàn)原理:調(diào)取手機(jī)攝像頭掃一掃功能實(shí)現(xiàn)
注:調(diào)取手機(jī)攝像頭需要再https協(xié)議
下才可以,有的項(xiàng)目啟動(dòng)配置https:true
可以實(shí)現(xiàn)啟動(dòng)的項(xiàng)目協(xié)議為https
且可以訪(fǎng)問(wèn),有的還是需要證書(shū)才能訪(fǎng)問(wèn)
實(shí)現(xiàn)
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('在運(yùn)運(yùn)單') router.push('order') } onUnmounted(() => { stop() }) const getCameras = () => { Html5Qrcode.getCameras() .then((devices) => { if (devices && devices.length) { isShow.value = true html5QrCode = new Html5Qrcode('reader') // start開(kāi)始掃描 start() } }) .catch((err) => { // handle err console.log('獲取設(shè)備信息失敗', err) // 獲取設(shè)備信息失敗 showToast('獲取設(shè)備信息失敗') }) } const start = () => { html5QrCode .start( {facingMode: "environment" }, { fps: 20, // 設(shè)置每秒多少幀 qrbox: { width: 250, height: 250 } // 設(shè)置取景范圍 // scannable, rest shaded. }, (decodedText, decodedResult) => { alert('掃碼結(jié)果' + decodedText) }, (errorMessage) => { // parse error, ideally ignore it. For example: // console.log(`QR Code no longer in front of camera.`); console.log('暫無(wú)額掃描結(jié)果', 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>
到此這篇關(guān)于html5項(xiàng)目實(shí)現(xiàn)掃描二維碼功能的文章就介紹到這了,更多相關(guān)html5掃描二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
基于MUI框架使用HTML5實(shí)現(xiàn)的二維碼掃描功能
這篇文章主要介紹了基于MUI框架使用HTML5實(shí)現(xiàn)的二維碼掃描功能,需要的朋友可以參考下2018-03-01HTML5實(shí)現(xiàn)分享到微信好友朋友圈QQ好友QQ空間微博二維碼功能
這篇文章主要介紹了HTML5實(shí)現(xiàn)分享到微信好友朋友圈QQ好友QQ空間微博二維碼功能的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-03HTML5混合開(kāi)發(fā)二維碼掃描以及調(diào)用本地?cái)z像頭
今天主管給了我個(gè)需求,說(shuō)要用混合開(kāi)發(fā),用H5調(diào)用本地?cái)z像頭進(jìn)行掃描二維碼,我之前有做過(guò)原生安卓的二維碼掃一掃,主要是通過(guò)調(diào)用zxing插件進(jìn)行操作的2017-12-27- 這篇文章主要介紹了Html5實(shí)現(xiàn)二維碼掃描并解析 的相關(guān)資料,需要的朋友可以參考下2016-01-20