# uni.scanCode(OBJECT)

Call up the code scanning interface of the client, and return the corresponding result after the code scanning is successful.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序 元服务 小红书小程序
x

# scanCode 兼容性

HarmonyOS
HBuilderX 4.23

OBJECT parameter description

Parameter Name Type Required Description Platform Difference Description
onlyFromCamera Boolean 是否只能从相机扫码,不允许从相册选择图片 抖音小程序、百度小程序、支付宝小程序不支持此参数
scanType Array 扫码类型,参考下方scanType的合法值 抖音小程序不支持此参数
autoDecodeCharset Boolean No Whether to enable the automatic character encoding function, the default is No App
autoZoom Boolean No Whether to enable auto zoom, enabled by default Only supported by App-Android (3.5.4+)
barCodeInput Boolean No Whether to support manual barcode input Only supported by Feishu MiniApp(V3.14.0)
hideAlbum Boolean No Whether to hide the album (not allowed to select pictures from the album), only scan the code from the camera. The default value is false. Only supported by Alipay MiniApp
success Function No The callback of the successful interface call. For the return content, please refer to the return parameter description.
fail Function No Callback function for interface call failure (triggered when identification fails, user cancels, etc.)
complete Function No The callback function of the end of the interface call (it will be executed when the call succeeds or fails)

Legal value of scanType

value description
barCode one-dimensional code
qrCode QR code
datamatrix Data Matrix code
pdf417 PDF417 Barcode

success return parameter description

Parameter Description Platform Difference Description
result 所扫码的内容
scanType 所扫码的类型 App、微信小程序、百度小程序、QQ小程序、京东小程序、支付宝小程序、小红书小程序
charSet 所扫码的字符集 App、微信小程序、百度小程序(所扫码的字符集,仅支持 Android 系统)、QQ小程序、京东小程序、小红书小程序
path 当所扫的码为当前应用的合法二维码时,会返回此字段,内容为二维码携带的 path。 微信小程序、QQ小程序、京东小程序、小红书小程序
rawData 原始数据,base64 编码 微信小程序、QQ小程序、京东小程序、支付宝小程序、小红书小程序
code Data obtained by scanning the code Alipay MiniApp
qrCode Return QR code data when scanning a QR code Alipay MiniApp
barCode Return barcode data when scanning a barcode Alipay MiniApp
imageChannel 来源 支付宝小程序
errMsg 小红书小程序

example

// Allow scanning codes from camera and album
uni.scanCode({
	success: function (res) {
		console.log('条码类型:' + res.scanType);
	}
});

// Only allow scanning codes through the camera
uni.scanCode({
	onlyFromCamera: true,
	success: function (res) {
		console.log('条码类型:' + res.scanType);
	}
});

// invoke barcode scanning
uni.scanCode({
	scanType: ['barCode'],
	success: function (res) {
		console.log('条码类型:' + res.scanType);
	}
});

Tip

  • Android/iOS的扫码引擎,使用zxing等开源库,识别效率较弱。如需更强的扫码效果,推荐使用uts插件:https://ext.dcloud.net.cn/search?q=%E6%89%AB%E7%A0%81&orderBy=Relevance&cat1=8&cat2=81。此外,uni-app x的扫码要优于uni-app很多,扫码效果、UI自定义性,都更好,升级uni-app x也是推荐选项。详情
  • If App-vue wants to customize the code scanning, please refer to How to use 5+ native interface controls in uni-app and plus.barcode API
  • App-nvue,支持barcode组件,可自定义扫码界面。详见。App端自定义扫码界面,建议使用nvue方式。
  • The WeChat MiniApp customize the code scanning interface, and the camera component can be used. See details
  • When the WeChat embedded browser runs the H5 version, you can scan the code through js sdk, you need to introduce a separate js, see details
  • Clicking back on the scan code interface will also enter the fail callback
  • Alipay MiniApp not support charSet, path in success callback
  • HX 3.4.4之后版本 android 新增 检测到 QR 码时自动放大功能,提升扫码识别率。