适用于uniappx项目的uts安卓原生扫码插件,谷歌MLKit机器学习扫描码-极速扫描二维码条形码

1,159 阅读1分钟

uniappx的uts扫码插件

适用于Android 平台

  1. 下载插件并导入你的项目
  2. 在你的页面import引入这个插件
  3. 制作自定义基座
  4. 运行自定义基座

uniapp插件市场链接直达 (ext.dcloud.net.cn/plugin?id=1…)

无需注册账号 无需添加应用 无需配置密钥 引入插件打包即用

接入的谷歌扫码器(ML Kit Barcode Scanning API) 能自动识别大多数标准格式 线性格式:Codabar、Code 39、Code 93、Code 128、EAN-8、EAN-13、ITF、UPC-A、UPC-E 2D 格式:Aztec、Data Matrix、PDF417、二维码 目前仅支持安卓 安卓版本需 >= 5.0

尽享丝滑的效果图

003.gif

lf-scan-code目录放在uni_modules下

index.uvue示例:


<template>
	<view class="content">
		<button class="scan-btn" @tap="scanCode">扫一扫</button>
		<view class="code-panel">
			<view class="code-label">
				<text>扫码结果:</text>
			</view>
			<view>
				<text class="code-value">{{codeResStr}}</text>
			</view>
		</view>
	</view>
</template>

<script lang="uts">
	import { initScanCode } from "../../uni_modules/lf-scan-code"
	export default {
		data() {
			return {
				codeResStr: ""
			}
		},
		onLoad() {

		},
		methods: {
			scanCode() {
				initScanCode().then((res : any) => {
					let resData = res as UTSJSONObject
					console.log(resData);
					if (resData['status'] == "success") {
						// 扫码成功结果
						this.codeResStr = resData['value'] as String;
					}
					if (resData['status'] == "cancel") {
						uni.showToast({
							title: "用户取消了操作",
							icon: "none"
						})
					}
					if (resData['status'] == "error") {
						console.log("报错了:", res['error']);
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		margin-top: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.scan-btn {
		background-color: pink;
		width: 300rpx;
		margin-top: 0;
		margin-right: auto;
		margin-bottom: 0;
		margin-left: auto;
	}

	.code-panel {
		margin-top: 200rpx;
	}
	
	.code-label{
		text-align: center;
	}
	.code-value{
		margin-top: 20rpx;
		color: mediumvioletred;
		font-size: 36rpx;
		text-align: center;
	}
</style>

iOS 平台

暂不支持

相关开发文档