H5扫描识别条形码

业务背景

uniapp框架,需要兼容微信小程序和H5环境,实现扫描条形码,再根据扫描的结果跳转到指定页面的功能。 uni.scanCode(OBJECT)支持小程序等,但是不兼容H5。

由于只需要扫描识别128类型的条形码,因此选择quagga2这个专门支持条形码的开源库来实现。 使用缺点:浏览器端,上传的二维码图片,需要将二维码部分框选出来。

实现思路

  1. 引入Quagga2库
  2. 根据环境选择图片来源: 移动端直接拍照识别,浏览器端通过上传图片来识别
  3. 识别条形码
  4. 处理识别结果

完整代码

js 复制代码
npm install @ericblade/quagga2
js 复制代码
<template>
	<view class="container">
		<view v-if="isScanning" class="scanning-container">
			<view id="scanner-container" class="scanner-view"></view>
			<button @click="stopScan" class="stop-button">停止扫描</button>
		</view>
	</view>
</template>

<script>
import Quagga from "@ericblade/quagga2";
export default {
	data() {
		return {
			isScanning: false,
			isMobile: false
		};
	},
	onLoad() {
		this.checkEnvironment();
	},
	methods: {
		// 检查设备类型
		async checkEnvironment() {
			try {
				const ua = navigator.userAgent.toLowerCase();
				this.isMobile = /mobile|android|iphone|ipad|phone/i.test(ua);
				// 判断当前环境: 移动端使用摄像头实时扫描,浏览器端使用图片上传
				if (this.isMobile) {
					await this.startCameraScan();
				} else {
					uni.showModal({
						content: "为了便于识别,请确保上传图片中,已经将条形码区域框选出来",
						title: "提示",
						success: async (res) => {
							if (res.confirm) {
								await this.uploadImageScan();
							} else uni.navigateBack();
						}
					});
				}
			} catch (error) {
				console.log("扫描启动失败", err);
				this.checkFail();
			}
		},
		// 启动摄像头扫描(移动端)
		async startCameraScan() {
			this.isScanning = true;
			// 等待视图更新
			await this.$nextTick();
			return new Promise((resolve, reject) => {
				Quagga.init(
					{
						inputStream: {
							name: "Live",
							type: "LiveStream",
							target: document.querySelector("#scanner-container")
						},
						decoder: {
							readers: ["code_128_reader"]//此处可继续添加支持的其他类型条形码,如:code_39_reader
						},
						locate: true,
						numOfWorkers: 2
					},
					(err) => {
						if (err) {
							console.error("Quagga初始化失败:", err);
							this.isScanning = false;
							this.checkFail();
							reject(err);
							return;
						}
						Quagga.start();
						resolve();
					}
				);
			});
		},
		// 图片上传扫描(浏览器端)
		async uploadImageScan() {
			try {
				const [file] = await new Promise((resolve, reject) => {
					uni.chooseImage({
						count: 1,
						sizeType: ["original"],
						sourceType: ["album"],
						success: (res) => resolve(res.tempFiles),
						fail: reject
					});
				});
				// 识别图片中的条形码
				const result = await new Promise((resolve, reject) => {
					Quagga.decodeSingle(
						{
							decoder: {
								readers: ["code_128_reader"]
							},
							locate: true,
							src: file.path
						},
						(result) => {
							if (result && result.codeResult) {
								resolve(result);
							} else {
								this.checkFail();
								reject(new Error("未识别到条形码"));
							}
						}
					);
				});
				this.handleScanResult(result);
			} catch (error) {
				console.error("图片识别失败:", error);
				this.checkFail();
			}
		},
		// 处理识别结果
		handleScanResult(result) {
			const code = result.codeResult.code;
			console.log("识别结果:", code)
		},
           //识别失败后的处理,返回上一页
		checkFail() {
			uni.navigateBack();
			setTimeout(() => {
				uni.showToast({
					title: "识别失败,请重试",
					icon: "error",
					duration: 2000
				});
			}, 1);
		},
		// 停止扫描
		stopScan() {
			if (this.isScanning && Quagga) {
				Quagga.stop();
				this.isScanning = false;
			}
		}
	},
	// 页面卸载时清理资源
	onUnload() {
		this.stopScan();
	}
};
</script>

<style lang="scss" scoped>
.container {
	padding: 20px;
	display: flex;
	flex-direction: column;
	align-items: center;
	.stop-button {
		margin: 10px 0;
		padding: 12px 24px;
		border: none;
		border-radius: 8px;
		font-size: 16px;
		cursor: pointer;
		background-color: #ff3b30;
		color: white;
	}
	.scanner-view {
		width: 100%;
		height: 300px;
		border: 2px solid #007aff;
		border-radius: 8px;
		overflow: hidden;
		margin-bottom: 20px;
	}
}
</style>
相关推荐
影子打怪3 小时前
uniapp项目中,通过renderjs的方式展示地图,及其标点、轨迹展示、轨迹回放
uni-app
iOS阿玮5 小时前
想偷懒购买现成的应用,结果一更新就遇到了4.3a!
uni-app·app·apple
HashTang6 小时前
【AI 编程实战】第 4 篇:一次完美 vs 五轮对话 - UnoCSS 配置的正确姿势
前端·uni-app·ai编程
雯0609~16 小时前
uni-app:防止重复提交
前端·javascript·uni-app
2501_9159090616 小时前
苹果应用加密方案的一种方法,在没有源码的前提下,如何处理 IPA 的安全问题
android·安全·ios·小程序·uni-app·iphone·webview
百锦再16 小时前
与AI沟通的正确方式——AI提示词:原理、策略与精通之道
android·java·开发语言·人工智能·python·ui·uni-app
2501_9159090616 小时前
iOS 项目中常被忽略的 Bundle ID 管理问题
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214316 小时前
iOS App 测试的工程化实践,多工具协同的一些尝试
android·ios·小程序·https·uni-app·iphone·webview
咸虾米_18 小时前
uniapp+unicloud实战项目,九两酒微信小程序商城及后台管理系统前后端部署运行步骤
微信小程序·uni-app·uniapp实战项目·unicloud云开发·vue3后台管理
怀君18 小时前
Uniapp——Android离线打包之更换启动屏和App图标
uni-app