uniapp——安卓扫码枪插件

869 阅读1分钟

zq-scancode插件使用说明

1、引入包

// 扫码插件
var scanCodeModule = uni.requireNativePlugin("zq-scancode-module")

2、调用方法

scanCodeModule.startScan({
	'name': '扫码',
    'showTip': true,
    'showLoading': true,
    'tip': "自定义扫码",
    'tipSize': 20,
    'tipColor': "#FF0000"
},
(ret) => {
	console.log("扫码内容:" + ret)
});

3、说明

startScan方法的参数:

第一个参数:json对象,根据实际情况传递,第二个参数会回传回来

参数名类型默认值说明
nameString扫码组件名称
showTipBooleantrue是否展示提示文字
tipString扫码中...提示文字
tipSizeNumber12提示文字大小
tipColorString#3B3B3C提示文字颜色
showLoadingBooleantrue是否展示 loading

第二个参数:扫码结果回调,返回json字符串,回调参数说明:

参数名说明
code状态码:200成功
msg提示信息,比如:扫码成功
data扫码结果
extend第一个参数回传

4、完整示例代码

<template>
	<div>
		<button type="primary" @click="scanCode">开始扫码</button>
	</div>
</template>

<script>
	// 获取 module 
	var scanCodeModule = uni.requireNativePlugin("zq-scancode-module")
	export default {
		onLoad() {
			plus.globalEvent.addEventListener('TestEvent', function(e) {
				console.log("TestEvent收到:" + e.msg)
			});
		},
		methods: {
			// 扫码
			scanCode() {
				scanCodeModule.startScan({
                    	'name': '扫码',
						'showTip': true,
						'showLoading': true,
						'tip': "自定义扫码",
						'tipSize': 20,
						'tipColor': "#FF0000"
					},
					(ret) => {
						console.log("扫码内容:" + ret)
					});
			}
		}
	}
</script>

5、补充说明

  • 扫码枪的原理是外接输入法(类似:外接键盘),通过连接蓝牙或USB线连接设备就可以使用,不需要申请系统权限