使用 html5-qrcode實(shí)現(xiàn)掃碼功能
發(fā)布時(shí)間:2023-09-13 16:45:31 作者:小張同學(xué)IT
我要評論

html5-qrcode是輕量級和跨平臺的QR碼和條形碼掃碼的JS庫,集成二維碼、條形碼和其他一些類型的代碼掃描功能,這篇文章主要介紹了使用 html5-qrcode 掃碼功能,需要的朋友可以參考下
介紹
html5-qrcode是輕量級和跨平臺的QR碼和條形碼掃碼的JS庫,集成二維碼、條形碼和其他一些類型的代碼掃描功能,代碼依賴于Zxing-js庫優(yōu)勢:
1、支持掃描不同類型的條形碼和二維碼
2、支持不同平臺,Android、IOS、MacOS、Windows或Linux
3、支持不同的瀏覽器,如Chrome、Firefox、Safari、Edge
4、支持相機(jī)掃描以及本地文件
5、支持自定義,如閃光/火炬支持、縮放等
注意:直接訪問攝像頭,涉及到隱私,所以環(huán)境必須是 `HTTPS`
實(shí)現(xiàn)
<template> <div class="home"> 這是首頁 <img alt="Vue logo" src="@/assets/logo.png"> </div> <hr/> <button @click="getCameras">掃碼</button> ==== <button @click="stop">取消掃碼</button> <div style="height: 100%; width: 100%"> <MyHeader :name="'調(diào)用攝像頭掃碼'" left="arrow-left" @goBackEv="$emit('goBack')" /> <div class="qrcode"> <div id="reader"></div> </div> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { Html5Qrcode } from "html5-qrcode" export default defineComponent({ name: 'Home', components: { }, setup(){ let html5QrCode:any = null const start = () => { html5QrCode.start( // environment后置攝像頭 user前置攝像頭 { facingMode: "environment" }, { fps: 2, // 可選,每秒幀掃描二維碼 qrbox: { width: 250, height: 250 } // 可選,如果你想要有界框UI // aspectRatio: 1.777778 // 可選,視頻饋送需要的縱橫比,(4:3--1.333334, 16:9--1.777778, 1:1--1.0)傳遞錯(cuò)誤的縱橫比會(huì)導(dǎo)致視頻不顯示 }, (decodedText: any, decodedResult: any) => { // do something when code is read console.log('decodedText', decodedText) console.log('decodedResult', decodedResult) // this.$emit("goBack", decodedText) } ) .catch((err: any) => { console.log('掃碼錯(cuò)誤信息', err) // 錯(cuò)誤信息處理僅供參考,具體情況看輸出!??! if (typeof err == 'string') { // this.$toast(err) } else { // if (err.name == 'NotAllowedError') return this.$toast("您需要授予相機(jī)訪問權(quán)限") // if (err.name == 'NotFoundError') return this.$toast('這個(gè)設(shè)備上沒有攝像頭') // if (err.name == 'NotSupportedError') return this.$toast('攝像頭訪問只支持在安全的上下文中,如https或localhost') // if (err.name == 'NotReadableError') return this.$toast('相機(jī)被占用') // if (err.name == 'OverconstrainedError') return this.$toast('安裝攝像頭不合適') // if (err.name == 'StreamApiNotSupportedError') return this.$toast('此瀏覽器不支持流API') } }) } const getCameras = () => { Html5Qrcode.getCameras() .then((devices) => { if (devices && devices.length) { html5QrCode = new Html5Qrcode("reader") start() } }) .catch((err:any) => { // handle err html5QrCode = new Html5Qrcode("reader") // this.$toast('您需要授予相機(jī)訪問權(quán)限') }) } const stop = () => { html5QrCode.stop().then((ignore:any) => { // QR Code scanning is stopped. console.log("QR Code scanning stopped.") }) .catch((err:any) => { // Stop failed, handle it. console.log("Unable to stop scanning.") }) } return { getCameras, stop } } }) </script>
結(jié)果
以上就是我對使用 html5-qrcode 掃碼的使用與總結(jié)
到此這篇關(guān)于使用 html5-qrcode實(shí)現(xiàn)掃碼功能的文章就介紹到這了,更多相關(guān)html5 qrcode 掃碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
HTML5打開手機(jī)掃碼功能及優(yōu)缺點(diǎn)
這篇文章主要介紹了HTML5打開手機(jī)掃碼功能及優(yōu)缺點(diǎn)的相關(guān)資料,需要的朋友可以參考下2017-11-27