VUE?html5-qrcode實(shí)現(xiàn)H5掃一掃功能實(shí)例
官方文檔 html5-qrcode
安裝 npm i html5-qrcode
1、新建一個(gè)組件
<div class="qrcode"> <div id="reader"></div> </div>
//樣式 .qrcode{ position: relative; height: 100%; width: 100%; background: rgba($color: #000000, $alpha: 0.48); } #reader{ top: 50%; left: 0; transform: translateY(-50%); }
2、引入
import { Html5Qrcode } from "html5-qrcode";
3、獲取攝像權(quán)限在created調(diào)用
getCameras() { Html5Qrcode.getCameras() .then((devices) => { if (devices && devices.length) { this.html5QrCode = new Html5Qrcode("reader"); this.start();//掃碼 } }) .catch((err) => { // handle err this.html5QrCode = new Html5Qrcode("reader"); this.error = "ERROR: 您需要授予相機(jī)訪問(wèn)權(quán)限" this.$emit("err",this.error) }); },
4、獲取掃碼內(nèi)容
start() { //environment后置 user前置 this.html5QrCode .start( {facingMode: "environment" }, { fps: 2,//500毫秒掃描一次 qrbox: { width: 250, height: 250 }, }, (decodedText, decodedResult) => { this.$emit("ok",decodedText) } ) .catch((err) => { console.log(`Unable to start scanning, error: ${err}`); }); },
5、必須在銷毀頁(yè)面前關(guān)閉掃碼功能否則會(huì)報(bào)錯(cuò) could not start video source
//銷毀前調(diào)用 beforeDestroy() { this.stop(); } //關(guān)閉掃碼 stop() { this.html5QrCode.stop().then((ignore) => { // QR Code scanning is stopped. console.log("QR Code scanning stopped."); }) .catch((err) => { // Stop failed, handle it. console.log("Unable to stop scanning."); }); },
6、在掃碼頁(yè)面引用組件
<BarScan ref="qrcode" @ok="getResult" @err="geterror" ></BarScan> getResult(e){ //e:掃碼內(nèi)容 } geterror(e){ //e:報(bào)錯(cuò)內(nèi)容 }
組件完整代碼
<template> <div class="qrcode"> <div id="reader"></div> </div> </template> <script> import { Html5Qrcode } from "html5-qrcode"; export default { created() { this.getCameras() }, beforeDestroy() { this.stop(); }, methods:{ getCameras() { Html5Qrcode.getCameras() .then((devices) => { if (devices && devices.length) { this.html5QrCode = new Html5Qrcode("reader"); this.start(); } }) .catch((err) => { // handle err this.html5QrCode = new Html5Qrcode("reader"); this.error = "ERROR: 您需要授予相機(jī)訪問(wèn)權(quán)限" this.$emit("err",this.error) }); }, start() { //environment后置 user前置 this.html5QrCode .start( {facingMode: "environment" }, { fps: 2, qrbox: { width: 250, height: 250 }, }, (decodedText, decodedResult) => { this.$emit("ok",decodedText) } ) .catch((err) => { this.$emit("err",err) }); }, stop() { this.html5QrCode.stop().then((ignore) => { // QR Code scanning is stopped. console.log("QR Code scanning stopped."); }) .catch((err) => { // Stop failed, handle it. console.log("Unable to stop scanning."); }); }, } } </script> <style lang="scss" scoped> .qrcode{ position: relative; height: 100%; width: 100%; background: rgba($color: #000000, $alpha: 0.48); } #reader{ top: 50%; left: 0; transform: translateY(-50%); } </style>
引用組件頁(yè)面
<BarScan ref="qrcode" @ok="getResult" @err="geterror" ></BarScan> import BarScan from '@/components/qrcode.vue' var _self; export default { components:{ BarScan }, methods:{ getResult(result){ this.result=result; }, geterror(e){ this.$toast(e) },} }
總結(jié)
到此這篇關(guān)于VUE html5-qrcode實(shí)現(xiàn)H5掃一掃功能的文章就介紹到這了,更多相關(guān)VUE html5-qrcode H5掃一掃內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用elementui的el-menu的折疊菜單collapse示例詳解
這篇文章主要介紹了vue使用elementui的el-menu的折疊菜單collapse示例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12vue中的虛擬dom知識(shí)點(diǎn)總結(jié)
這篇文章主要介紹了vue中的虛擬dom知識(shí)點(diǎn)總結(jié),文章圍繞主題內(nèi)容展開詳細(xì)介紹,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-04-04Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼)
這篇文章主要介紹了Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼),需要的朋友可以參考下2018-02-02解決vue路由name同名,路由重復(fù)的問(wèn)題
這篇文章主要介紹了解決vue路由name同名,路由重復(fù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Vue實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04