VUE html5-qrcode H5扫一扫功能

3 篇文章 0 订阅

官方文档  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、获取摄像权限在created调用

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: 您需要授予相机访问权限"
					  this.$emit("err",this.error)
			        });
},

4、获取扫码内容

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、必须在销毁页面前关闭扫码功能否则会报错  could not start video source

//销毁前调用
beforeDestroy() {
		    this.stop();
}

//关闭扫码
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:扫码内容
}

geterror(e){
    //e:报错内容
}

组件完整代码

<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: 您需要授予相机访问权限"
					  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)
			},}
}

  • 8
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
### 回答1: vue-qrcode-reader是一个基于Vue.js的条形码扫描组件。它为用户提供了在网页上扫描条形码的功能。 使用vue-qrcode-reader,我们需要先安装并导入组件。然后,我们可以在Vue的模板中使用组件,并通过设定相应的参数来调整扫描的设置。 该组件支持从摄像头扫描条形码,并可以将扫描到的条形码数据在网页展示出来。我们可以通过捕获组件的事件来获取扫描到的条形码数据,并进行后续的处理。 在使用vue-qrcode-reader的过程中,我们可以自定义组件的样式、设置摄像头的类型、调整扫描的精度等。此外,组件还提供了一些其他的参数,如自动开始扫描、扫描完成后是否暂停等,以便根据实际需求进行相应的调整。 使用vue-qrcode-reader可以方便地在Vue项目中实现条形码的扫描功能。它简化了扫描条形码的开发过程,提高了用户体验,并可以应用于各种需要条形码扫描的场景,如电商购物车、门禁系统等。 ### 回答2: vue-qrcode-reader是一个用于Vue.js的条形码扫描器插件。它基于QuaggaJS库,并提供简单易用的接口,让我们能够在Vue.js应用程序中轻松地集成条形码扫描功能。 使用vue-qrcode-reader非常简便。首先,我们需要安装该插件,可以在命令行中运行`npm install vue-qrcode-reader`来进行安装。安装完成后,可以在Vue组件中引入并注册该插件。 在使用vue-qrcode-reader之前,我们需要在页面中准备一个用于显示扫描结果的容器元素。可以使用一个`div`元素,并在其上绑定一个自定义的`class`或`id`,以便在Vue组件中引用。 然后,在Vue组件中,我们需要导入vue-qrcode-reader插件,并在`components`选项中进行注册。然后,我们可以在组件的`template`中使用插件提供的`Scanner`组件,来实现条形码的扫描功能。 在`Scanner`组件中,我们可以使用`@scan-success`事件来监听扫描成功的回调函数,并处理扫描到的条形码数据。我们还可以通过设置`@picker-active-red-line-color`属性来改变扫描框的红色边线颜色,以及使用`@activate-vibrate`属性来设置设备在扫描时是否震动反馈。 最后,在Vue组件中,我们可以使用`this.$refs.scanner.start`方法来启动条形码扫描,使用`this.$refs.scanner.stop`方法来停止条形码扫描。 总结而言,vue-qrcode-reader是一个方便易用的Vue.js条形码扫描器插件,可以轻松地集成到Vue.js应用程序中,实现条形码的扫描功能。 ### 回答3: vue-qrcode-reader 是一款基于 Vue.js 的扫码组件,可以用于扫描条形码。它使用了浏览器的摄像头,通过扫描条形码获取条形码的信息。 使用 vue-qrcode-reader 很简单,只需要在 Vue.js 的项目中导入该组件,并在需要扫描条形码的地方添加该组件即可。通过调用组件的方法,可以解析并获取扫描到的条形码信息。 在使用 vue-qrcode-reader 的时候,可以根据需要进行设置,如配置是否启用闪光灯、设置摄像头的分辨率等。扫描到条形码后,可以通过回调函数获取到扫描到的条形码的内容。 vue-qrcode-reader 提供了一套功能完善的 API,可以方便地进行二维码的扫描和解析。同时,它还支持多种条形码的解析,如 EAN、UPC、Code39 等。 总的来说,vue-qrcode-reader 是一款方便实用的扫码组件,可以在 Vue.js 项目中轻松实现条形码的扫描功能,为用户提供更加便捷的条形码操作体验。
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值