亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VUE?html5-qrcode實(shí)現(xiàn)H5掃一掃功能實(shí)例

 更新時(shí)間:2023年08月05日 08:37:37   作者:改bug的101天  
這篇文章主要給大家介紹了關(guān)于VUE?html5-qrcode實(shí)現(xiàn)H5掃一掃功能的相關(guān)資料,html5-qrcode是輕量級(jí)和跨平臺(tái)的QR碼和條形碼掃碼的JS庫(kù),集成二維碼、條形碼和其他一些類型的代碼掃描功能,需要的朋友可以參考下

官方文檔  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示例詳解

    這篇文章主要介紹了vue使用elementui的el-menu的折疊菜單collapse示例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-12-12
  • vue better-scroll插件使用詳解

    vue better-scroll插件使用詳解

    本篇文章主要介紹了vue better-scroll插件使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • vue中的虛擬dom知識(shí)點(diǎn)總結(jié)

    vue中的虛擬dom知識(shí)點(diǎn)總結(jié)

    這篇文章主要介紹了vue中的虛擬dom知識(shí)點(diǎn)總結(jié),文章圍繞主題內(nèi)容展開詳細(xì)介紹,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-04-04
  • Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼)

    Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼)

    這篇文章主要介紹了Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼),需要的朋友可以參考下
    2018-02-02
  • Vue學(xué)習(xí)筆記之表單輸入控件綁定

    Vue學(xué)習(xí)筆記之表單輸入控件綁定

    本篇文章主要介紹了Vue學(xué)習(xí)筆記之表單輸入綁定,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • vue路由跳轉(zhuǎn)傳參數(shù)的方法

    vue路由跳轉(zhuǎn)傳參數(shù)的方法

    這篇文章主要介紹了vue路由跳轉(zhuǎn)傳參數(shù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vue中mixins的使用方法詳解

    Vue中mixins的使用方法詳解

    mixins是一種分發(fā) Vue 組件中可復(fù)用功能的使用方式,它是一個(gè) js 對(duì)象,包含我們組件script中的任意功能選項(xiàng),下面就跟隨小編一起來(lái)看看它的具體使用吧
    2024-03-03
  • 理理Vue細(xì)節(jié)(推薦)

    理理Vue細(xì)節(jié)(推薦)

    這篇文章主要介紹了Vue細(xì)節(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 解決vue路由name同名,路由重復(fù)的問(wèn)題

    解決vue路由name同名,路由重復(fù)的問(wèn)題

    這篇文章主要介紹了解決vue路由name同名,路由重復(fù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Vue實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器

    Vue實(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

最新評(píng)論