vue3 html5-qrcode 实现扫描二维码 仿照wx扫码样式

直接上代码:npm i html5-qrcode

<van-button class="scanButton" @click="stop" type="success">结束扫码</van-button>

<van-search v-model="searchName" show-action label="" placeholder="请输入姓名" @search="onSearch">
				<template #action>
					<div @click="getCameras">
						<van-icon name="scan" />
						扫码报到
					</div>
				</template>
			</van-search>

import { Html5Qrcode } from 'html5-qrcode';

const getCameras = () => {
	scanCodeSta.value = true;
	// showToast('正在初始化扫码,请稍后');
	showToast({
		message: '正在初始化扫码,请稍后',
		position: 'bottom',
	});

	Html5Qrcode.getCameras().then((devices) => {
		if (devices && devices.length) {
			// 如果有2个摄像头,1为前置的
			if (devices.length > 1) {
				cameraId.value = devices[1].id;
			} else {
				cameraId.value = devices[0].id;
			}
			devicesInfo.value = devices;
			// start开始扫描
			start();
		}
	}).catch((err) => {
		console.log('获取设备信息失败', err); // 获取设备信息失败
		showToast('获取设备信息失败');
	});
}
const start = () => {
	html5QrCode.value = new Html5Qrcode('reader');
	setTimeout(() => {
		closeToast();
	}, 700);
	html5QrCode.value.start(cameraId.value, {
		focusMode: 'continuous', //设置连续聚焦模式
		// fps: 10, // 设置每秒多少帧
		fps: 5, //设置扫码识别速度
		// qrbox: { width: '100%', height: '100%' }, // 设置取景范围
		qrbox: { width: 320, height: 320 }, // 设置取景范围
		// aspectRatio: 1.777778, // 可选,视频馈送需要的纵横比,(4:3--1.333334, 16:9--1.777778, 1:1--1.0)传递错误的纵横比会导致视频不显示
	}, (decodedText, decodedResult) => {
		// showToast('扫描的结果===', decodedText, decodedResult);
		result.value = decodedText;
		if (decodedText) {
			stop();
			handleScanCheckin();
		}
	}, (errorMessage) => {
		// showToast('暂无扫描结果');
		console.log('暂无扫描结果', errorMessage);
	}).catch((err) => {
		console.log(`Unable to start scanning, error: ${err}`);
	});
}
const stop = () => {
	scanCodeSta.value = false;
	if (!html5QrCode.value) {
		return;
	}
	html5QrCode.value.stop().then((ignore) => {
		console.log('QR Code scanning stopped.', ignore);
	}).catch((err) => {
		console.log('Unable to stop scanning.', err);
	});
}

:deep(#qr-shaded-region) {
	overflow: hidden;
}

:deep(#qr-shaded-region::after) {
	position: absolute;
	content: '';
	width: 100%;
	top: 0;
	left: 0;
	transform: translateY(-100%);
	height: 50px;
	background: linear-gradient(180deg, rgba(0, 255, 51, 0) 43%, #00ff33 211%);
	border-bottom: 1px solid #00ff33;
	animation: scan-animation 2s linear infinite;
}

@keyframes scan-animation {
	0% {
		top: 0;
	}

	100% {
		top: 100%;
	}
}

相关推荐

  1. vue3 html5-qrcode 实现扫描 仿照wx样式

    2024-04-27 07:36:02       17 阅读
  2. vue qrcode生成

    2024-04-27 07:36:02       21 阅读
  3. 使用 Vue 3qrcode.js 开发显示组件

    2024-04-27 07:36:02       8 阅读
  4. 利用qrcode.vue库生成

    2024-04-27 07:36:02       35 阅读
  5. Vue中生成,使用现有的qrcode

    2024-04-27 07:36:02       12 阅读
  6. 【taro react】 ---- QRCode 生成

    2024-04-27 07:36:02       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-27 07:36:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-27 07:36:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-27 07:36:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-27 07:36:02       20 阅读

热门阅读

  1. Spark 为什么比 Hive 快

    2024-04-27 07:36:02       11 阅读
  2. C#面:ASP.NET 与 ASP 相比,主要有哪些进步

    2024-04-27 07:36:02       12 阅读
  3. 怎么理解Fiber,Fiber解决了什么问题

    2024-04-27 07:36:02       17 阅读
  4. 【2024.04.27_晴】

    2024-04-27 07:36:02       15 阅读
  5. R语言 数据框的长、宽格式转换

    2024-04-27 07:36:02       26 阅读
  6. 应用回归分析,R语音,逐步回归法,第5章

    2024-04-27 07:36:02       38 阅读
  7. 从npm到yarn,再到pnpm

    2024-04-27 07:36:02       16 阅读
  8. docker部署前端项目(四)

    2024-04-27 07:36:02       16 阅读
  9. NPM简介

    2024-04-27 07:36:02       14 阅读
  10. Mac运行npm run serve报错opensslErrorStack

    2024-04-27 07:36:02       16 阅读
  11. B站秒杀项目学习心得

    2024-04-27 07:36:02       16 阅读