# uni.scanCode(options?)

扫码

# scanCode 兼容性

Web 微信小程序 Android Android uni-app x UTS 插件 iOS iOS uni-app x UTS 插件 HarmonyOS
x 4.41 x x x x 4.61

App平台暂未内置,见插件市场

# 参数

名称 类型 必填 默认值 兼容性 描述
options ScanCodeOptions - - uni.scanCode参数定义
名称 类型 必备 默认值 兼容性 描述
onlyFromCamera boolean -
是否只能从相机扫码,不允许从相册选择图片
scanType Array<'barCode' | 'qrCode' | 'datamatrix' | 'pdf417'> -
扫码类型
success (res: ScanCodeSuccess) => void -
uni.scanCode成功回调函数定义
fail (res: ScanCodeFail) => void -
uni.scanCode失败回调函数定义
complete (res: any) => void -
uni.scanCode完成回调函数定义

# ScanCodeSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
result string - - -
scanType string - - -
charSet string -
所扫码的字符集
path string -
当所扫的码为当前小程序二维码时,会返回此字段,内容为二维码携带的 path
rawData string -
原始数据,base64编码

# 示例

hello uni-app x

该 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: 50rpx;
		line-height: 50rpx;
	}
</style>

# 参见

# 示例

hello uni-app x

该 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: 50rpx;
		line-height: 50rpx;
	}
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息