
简体中文
扫码(包括一维码和二维码)
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
x | 4.41 | 4.71 | 4.71 | 4.71 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | ScanCodeOptions | 否 | - | uni.scanCode参数定义 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
result | string | 是 | - | ||
scanType | string | 是 | - | ||
charSet | string | 否 | - | 所扫码的字符集 | |
path | string | 否 | - | 当所扫的码为当前小程序二维码时,会返回此字段,内容为二维码携带的 path | |
rawData | string | 否 | - | 原始数据,base64编码 |
示例为 alpha 分支:hello uni-app x(alpha)
master 分支:hello uni-app x(master)
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-title">扫码结果:</view>
<view v-if="result" class="scan-result">
{{result}}
</view>
<view class="uni-btn-v">
<button type="primary" @click="scan">扫一扫</button>
</view>
</view>
</view>
</template>
<script lang="uts">
export default {
data() {
return {
title: 'scanCode',
result: ''
}
},
methods: {
scan() {
uni.scanCode({
success: (res: ScanCodeSuccess) => {
console.log('res: ',res);
this.result = res.result
},
fail: (err: ScanCodeFail) => {
console.log('err: ',err);
// 需要注意的是小程序扫码不需要申请相机权限
}
});
}
}
}
</script>
<style>
.scan-result {
min-height: 25px;
line-height: 25px;
}
</style>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |
Android端实现扫码所使用的依赖库
"androidx.camera:camera-core:1.4.1",
"com.google.mlkit:barcode-scanning:17.2.0",
"com.github.albfernandez:juniversalchardet:2.0.4"
iOS端实现扫码所使用的依赖库
pod 'GoogleMLKit/BarcodeScanning', '6.0.0'