vue實(shí)現(xiàn)h5掃碼的代碼示例
插件 html5-qrcode npm地址
html5-qrcode是一個(gè)基于JavaScript 輕量級(jí)和跨平臺(tái)的掃碼插件。允許用戶使用攝像頭掃描二維碼,并且解析為文本或者url。
- 支持掃描不同類型的二維碼和條形碼
- 支持不同的平臺(tái),Android、IOS、Windows、MacOs或者linux
- 支持不同的瀏覽器,Chrome、Safari、Edge等
- 支持掃描本地文件
訪問(wèn)攝像頭涉及用戶隱私,所以訪問(wèn)環(huán)境必須在https下
實(shí)現(xiàn)(該代碼環(huán)境基于vue3)
- 安裝依賴
npm install html5-qrcode
- 引入
import { Html5Qrcode } from 'html5-qrcode'
- 使用
setup數(shù)據(jù)
const state = reactive({ html5QrCode: null, fileList:[] })
- 判斷當(dāng)前環(huán)境下是否有攝像頭
Html5Qrcode.getCameras() .then(devices =>{ if(devices &&devices.length){ // 當(dāng)前環(huán)境下能識(shí)別出攝像頭,并且攝像頭的數(shù)據(jù)可能不止一個(gè) state.html5QrCode = new Html5Qrcode('reader') // reader 放置掃碼功能的元素ID startInit() } }) .catch(() =>{ // 攝像頭無(wú)訪問(wèn)權(quán)限 })
- 掃碼
const startInit = () =>{ state.html5QrCode.start( // environment后置攝像頭 user前置攝像頭 {facingMode:'environment'}, { fps:1, // 可選,每n秒幀掃描一次 qrbox:{ width:250, height:250 } // 掃描的 UI框 }, (decodedText, decodedResult) =>{ // 掃描結(jié)果 } ) .catch((err) =>{ // 掃碼錯(cuò)誤信息 let message = '' if(typeof err == 'string'){ message = '識(shí)別失敗' }else { if (err.name == 'NotAllowedError') { message = '您需要授予相機(jī)訪問(wèn)權(quán)限!' } if (err.name == 'NotFoundError') { message = '這個(gè)設(shè)備上沒(méi)有攝像頭!' } if (err.name == 'NotSupportedError') { message = '攝像頭訪問(wèn)只支持在安全的上下文中,如https或localhost!' } if (err.name == 'NotReadableError') { message = '相機(jī)被占用!' } if (err.name == 'OverconstrainedError') { message = '安裝攝像頭不合適!' } if (err.name == 'StreamApiNotSupportedError') { message = '此瀏覽器不支持流API!' } } }) }
- 識(shí)別本地文件
const dealSelectFiles = () => { try { window.qrcode.callback = (result) => { // 識(shí)別成功 } // get select files. let file = state.fileList[0].file var reader = new FileReader() reader.onload = (function () { return function (e) { window.qrcode.decode(e.target.result) } })(file) reader.readAsDataURL(file) } catch (error) { // 識(shí)別失敗 } }
- 在離開頁(yè)面時(shí)要停止掃碼功能
onUnmounted(() => { //掃描設(shè)備是否在運(yùn)行 if (state.html5QrCode.isScanning) { stop() } })
完整代碼
<template> <div class="scanCode"> <div class="container"> <div class="qrcode"> <div id="reader"></div> </div> </div> <div class="btn"> <div class="left-back"> <van-icon name="arrow-left" @click="clickBack" /> </div> ? <div class="right-file"> <van-uploader v-model="fileList" :preview-image="false" :after-read="dealSelectFiles"> <van-icon name="photo-o" /> </van-uploader> </div> </div> </div> </template> ? <script> import { reactive } from 'vue' import { defineComponent, toRefs, onMounted, onUnmounted } from 'vue' import { Html5Qrcode } from 'html5-qrcode' import { showToast, } from 'vant' export default defineComponent({ setup() { const state = reactive({ html5QrCode: null, fileList: [], }) ? ? const start = () => { state.html5QrCode .start( { facingMode: 'environment' }, { fps: 1, qrbox: { width: 250, height: 250 } }, (decodedText, decodedResult) => { console.log('decodedText', decodedText) console.log('decodedResult', decodedResult) } ) .catch((err) => { console.log('掃碼錯(cuò)誤信息', err) let message = '' // 錯(cuò)誤信息處理僅供參考,具體描述自定義 if (typeof err == 'string') { message = '二維碼識(shí)別失??!' } else { if (err.name == 'NotAllowedError') { message = '您需要授予相機(jī)訪問(wèn)權(quán)限!' } if (err.name == 'NotFoundError') { message = '這個(gè)設(shè)備上沒(méi)有攝像頭!' } if (err.name == 'NotSupportedError') { message = '攝像頭訪問(wèn)只支持在安全的上下文中,如https或localhost!' } if (err.name == 'NotReadableError') { message = '相機(jī)被占用!' } if (err.name == 'OverconstrainedError') { message = '安裝攝像頭不合適!' } if (err.name == 'StreamApiNotSupportedError') { message = '此瀏覽器不支持流API!' } } }) } ? const getCameras = () => { Html5Qrcode.getCameras() .then((devices) => { if (devices && devices.length) { state.html5QrCode = new Html5Qrcode('reader') start() } }) .catch((err) => { showToast({ message: '攝像頭無(wú)訪問(wèn)權(quán)限!', duration: 3000 }) } ? const stop = () => { state.html5QrCode .stop() .then((ignore) => { console.log('停止掃碼', ignore) }) .catch((err) => { console.log(err) showToast('停止掃碼失敗') }) } ? const dealSelectFiles = () => { try { window.qrcode.callback = (result) => { showToast('成功了,結(jié)果是:' + result) } // get select files. let file = state.fileList[0].file var reader = new FileReader() reader.onload = (function () { return function (e) { window.qrcode.decode(e.target.result) } })(file) reader.readAsDataURL(file) } catch (error) { showToast({ message: '圖片識(shí)別失?。?, duration: 3000 }) } } ? onMounted(() => { getCameras() }) ? ? onUnmounted(() => { //掃描設(shè)備是否在運(yùn)行 if (state.html5QrCode.isScanning) { stop() } }) return { ...toRefs(state), getCameras, dealSelectFiles, } } }) </script> ? <style lang="scss" scoped> .scanCode { height: 100vh; display: flex; flex-direction: column; background: rgba(0, 0, 0); } .container { height: 90vh; position: relative; width: 100%; } ? .qrcode { height: 100%; } #reader { top: 50%; left: 0; transform: translateY(-50%); } ? .btn { flex: 1; padding: 3vw; display: flex; justify-content: space-around; color: #fff; font-size: 8vw; align-items: flex-start; } </style> ?
最終效果
以上就是vue實(shí)現(xiàn)h5掃碼的代碼示例的詳細(xì)內(nèi)容,更多關(guān)于vue實(shí)現(xiàn)h5掃碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于dev-tool安裝方法(手動(dòng)安裝版)
這篇文章主要介紹了關(guān)于dev-tool安裝方法(手動(dòng)安裝版),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue路由傳參之使用query傳參頁(yè)面刷新數(shù)據(jù)丟失問(wèn)題解析
這篇文章主要介紹了vue路由傳參使用query傳參頁(yè)面刷新數(shù)據(jù)丟失問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04vue使用element實(shí)現(xiàn)上傳圖片和修改圖片功能
前幾天做到一個(gè)關(guān)于圖片上傳功能,下面這篇文章主要給大家介紹了關(guān)于vue使用element實(shí)現(xiàn)上傳圖片和修改圖片功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue+elementUI實(shí)現(xiàn)表格關(guān)鍵字篩選高亮
這篇文章主要為大家詳細(xì)介紹了vue+elementUI實(shí)現(xiàn)表格關(guān)鍵字篩選高亮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05VUE+Canvas實(shí)現(xiàn)財(cái)神爺接元寶小游戲
這篇文章主要介紹了VUE+Canvas實(shí)現(xiàn)財(cái)神爺接元寶小游戲,需要的朋友可以參考下本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-04-04vue.js移動(dòng)數(shù)組位置,同時(shí)更新視圖的方法
下面小編就為大家分享一篇vue.js移動(dòng)數(shù)組位置,同時(shí)更新視圖的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Vue的filters(本地)或filter(全局)過(guò)濾常用數(shù)據(jù)類型解析
這篇文章主要介紹了Vue的filters(本地)或filter(全局)過(guò)濾常用數(shù)據(jù)類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue3應(yīng)用elementPlus table并滾動(dòng)顯示問(wèn)題
這篇文章主要介紹了vue3應(yīng)用elementPlus table并滾動(dòng)顯示問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12