欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

官方文檔  html5-qrcode

安裝   npm i html5-qrcode

1、新建一個組件 

<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: 您需要授予相機訪問權(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、必須在銷毀頁面前關(guān)閉掃碼功能否則會報錯  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、在掃碼頁面引用組件

<BarScan ref="qrcode" @ok="getResult" @err="geterror" ></BarScan>
getResult(e){
    //e:掃碼內(nèi)容
}
geterror(e){
    //e:報錯內(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: 您需要授予相機訪問權(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>

引用組件頁面

<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實現(xiàn)H5掃一掃功能的文章就介紹到這了,更多相關(guān)VUE html5-qrcode H5掃一掃內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue使用elementui的el-menu的折疊菜單collapse示例詳解

    vue使用elementui的el-menu的折疊菜單collapse示例詳解

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

    vue better-scroll插件使用詳解

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

    vue中的虛擬dom知識點總結(jié)

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

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

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

    Vue學習筆記之表單輸入控件綁定

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

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

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

    Vue中mixins的使用方法詳解

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

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

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

    解決vue路由name同名,路由重復的問題

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

    Vue實現(xiàn)簡單網(wǎng)頁計算器

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)簡單網(wǎng)頁計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評論