::: sourceCode
## uni.scanCode(options?) @scancode
> issues: https://issues.dcloud.net.cn/?mid=api.device.scanCode
> 源码: https://github.com/dcloudio/uni-app/tree/uni-app-x-alpha/src/uni_modules/uni-scanCode
:::
扫码(包括一维码和二维码)
### scanCode 兼容性
### 参数
| 名称 | 类型 | 必填 | 兼容性 | 描述 |
| :- | :- | :- | :-: | :- |
| options | **ScanCodeOptions** | 否 | Web: x | uni.scanCode参数定义 |
#### options 的属性描述
| 名称 | 类型 | 必备 | 兼容性 | 描述 |
| :- | :- | :- | :-: | :- |
| onlyFromCamera | boolean | 否 | Web: x | 是否只能从相机扫码,不允许从相册选择图片 |
| scanType | Array<string> | 否 | Web: x | 扫码类型 |
| success | (res: [ScanCodeSuccess](#scancodesuccess-values)) => void | 否 | Web: x | uni.scanCode成功回调函数定义 |
| fail | (res: ScanCodeFail) => void | 否 | Web: x | uni.scanCode失败回调函数定义 |
| complete | (res: any) => void | 否 | Web: x | uni.scanCode完成回调函数定义 |
##### scanType 的属性描述
| 合法值 | 兼容性 |
| :- | :-: |
| "barCode" | Web: x; 微信小程序: 4.41; 支付宝小程序: 5.31; Android: 4.71; iOS: 4.71; HarmonyOS: 4.61 |
| "qrCode" | Web: x; 微信小程序: 4.41; 支付宝小程序: 5.31; Android: 4.71; iOS: 4.71; HarmonyOS: 4.61 |
| "datamatrix" | Web: x; 微信小程序: 4.41; 支付宝小程序: 5.31; Android: 4.71; iOS: 4.71; HarmonyOS: 4.61 |
| "pdf417" | Web: x; 微信小程序: 4.41; 支付宝小程序: 5.31; Android: 4.71; iOS: 4.71; HarmonyOS: 4.61 |
#### ScanCodeSuccess 的属性值 @scancodesuccess-values
| 名称 | 类型 | 必备 | 兼容性 |
| :- | :- | :- | :-: |
| result | string | 是 | Web: x |
| scanType | string | 是 | Web: x |
### 示例
示例为[hello uni-app x alpha分支](https://gitcode.com/dcloud/hello-uni-app-x/blob/prod_alpha/pages/API/scan-code/scan-code.uvue),与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例[另见](https://gitcode.com/dcloud/hello-uni-app-x/blob/master//pages/API/scan-code/scan-code.uvue)
>
> 该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
::: preview
> appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/scan-code/scan-code
```uvue
扫码结果:
{{result}}
```
:::
## 通用类型
### GeneralCallbackResult @generalcallbackresult-values
| 名称 | 类型 | 必备 | 描述 |
| :- | :- | :- | :- |
| errMsg | string | 是 | 错误信息 |
### 平台实现说明
- uni-app x的Android/iOS平台的扫码基于Google的机器学习库,对各种一维、二维码都有较好的识别效果。超过了uni-app的扫码。
扫码API,其实是一个开源的uvue页面,页面中内嵌了[camera组件](../component/camera.md),camera组件提供扫码模式。源码在文档上方点击右侧gitcode或github。\
如需连续扫码,推荐使用[camera组件](../component/camera.md)\
扫码需要摄像头权限,扫码界面的相册图标点击后需要相册读取权限。部分Android应用商店要求权限申请前要声明,可以使用[uni-registerRequestPermissionTips插件](https://ext.dcloud.net.cn/plugin?name=uni-registerRequestPermissionTips)
- 鸿蒙、小程序直接调用了其平台提供的扫码API,UI不可自定义。但各种一维、二维码均可识别。
### 依赖库版本
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'
```