---
name: "scanCode"
mode: "kit"
versionRequirements:
  - { name: "BaseKit", version: "2.3.2" }
  - { name: "@ray-js/ray", version: "0.3.23" }
platform:
  - "iOS"
  - "Android"
async: true
---

## scanCode

> [VERSION] BaseKit >= 2.3.2 | @ray-js/ray >= 0.3.23

> [PLATFORM] iOS, Android

> ⚡ **Supports Promise** — Returns a Promise when success / fail / complete callbacks are omitted.

### Description

Launch the client scan UI for scanning. Note: On Android, only the result field is usable; other fields are meaningless.

### Parameters

| Property | Type | Required | Default | Since | Description |
| --- | --- | --- | --- | --- | --- |
| `onlyFromCamera` | `boolean` | No | `false` | `2.3.2` | Whether to scan only using the camera; selecting images from the album is not allowed. |
| `isShowActionTitle` | `boolean` | No | `true` | `3.3.3` | Whether to show the action title (Android only) |
| `isShowTorch` | `boolean` | No | `false` | `3.3.3` | Whether to display the flashlight (Android only) |
| `isShowKeyboard` | `boolean` | No | `false` | `3.12.0` | Whether to show the input for the setup code |
| `keyboardBean` | `KeyboardBean` | No | - | `3.12.0` | Custom text for the setup-code input |
| `customTips` | `string` | No | - | `3.3.3` | Custom prompt text (Android only) |
| `scanType` | `string[]` | No | `["barCode", "qrCode"]` | `2.3.2` | Scan type |
| `complete` | `() => void` | No | - | - | Completion callback (executed on both success and failure) |
| `success` | `(params: Object) => void` ↓see below | No | - | - | Callback on successful API call |
| `fail` | `(params: Object) => void` ↓see below | No | - | - | Failure callback |

#### success callback parameters

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `result` | `string` | `3.2.6` | Scanned content |
| `scanType` | `string` | `3.2.6` | Type of the scanned code |
| `charSet` | `string` | `3.2.6` | Character set of the scanned code |
| `path` | `string` | `3.2.6` | If the scanned code is the current Mini Program QR code, this field is returned with the QR code’s path (may be absent). |
| `rawData` | `string` | `3.2.6` | Raw data, base64-encoded |

#### fail callback parameters

| Property | Type | Description |
| --- | --- | --- |
| `errorMsg` | `string` | Error message |
| `errorCode` | `string \| number` | Error code |
| `innerError` | `Object` | Error extension |

##### fail(params).innerError properties

| Property | Type | Description |
| --- | --- | --- |
| `errorCode` | `string \| number` | Error extension code |
| `errorMsg` | `string` | Error extension message |


### Referenced Types

##### `interface` KeyboardBean

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `title` | `string` | `3.12.0` | Keyboard title && input page title |
| `placeholder` | `string` | `3.12.0` | Input page input field placeholder |
| `desc` | `string` | `3.12.0` | Input description on the input page |
| `actionText` | `string` | `3.12.0` | Text for action buttons on the input page |


### Examples

#### Demo

```tsx
import { scanCode } from '@ray-js/ray'

// Open the client scan UI; after scanning, the callback returns the content and type (on Android only the result field is valid)
scanCode({
  onlyFromCamera: false,
  isShowActionTitle: true,
  isShowTorch: false,
  isShowKeyboard: false,
  scanType: ["barCode", "qrCode"],
  success: data => console.log("Scan result:", data.result, data.scanType),
  fail: error => console.error(error),
});
```
