|
2 | 2 |
|
3 | 3 | A powerful React Native OCR (Optical Character Recognition) module powered by Google ML Kit. Supports multiple languages and scripts with selective model loading for optimized app size. |
4 | 4 |
|
5 | | -## Installation |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🌍 **Multi-language support**: Latin, Chinese, Devanagari, Japanese, and Korean scripts |
| 8 | +- 📦 **Selective model loading**: Include only the languages you need to minimize app size |
| 9 | +- ⚡ **High performance**: Powered by Google ML Kit's on-device text recognition |
| 10 | +- 🔄 **Flexible deployment**: Choose between bundled models (offline) or unbundled models (download on demand) |
| 11 | +- 📱 **Cross-platform**: Works on both iOS and Android |
6 | 12 |
|
| 13 | +## Requirements |
7 | 14 |
|
8 | | -```sh |
| 15 | +- iOS 15.5+ |
| 16 | +- Android API 21+ |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
9 | 21 | npm install rn-mlkit-ocr |
| 22 | +# or |
| 23 | +yarn add rn-mlkit-ocr |
| 24 | +``` |
| 25 | + |
| 26 | +### iOS Setup |
| 27 | + |
| 28 | +Run pod install: |
| 29 | + |
| 30 | +```bash |
| 31 | +cd ios && pod install |
| 32 | +``` |
| 33 | + |
| 34 | +### Android Setup |
| 35 | + |
| 36 | +No additional setup required for Android. |
| 37 | + |
| 38 | +## Configuration |
| 39 | + |
| 40 | +### Selecting OCR Models |
| 41 | + |
| 42 | +By default, all language models are included. To optimize your app size, you can specify which models to include. |
| 43 | + |
| 44 | +#### For Expo Projects |
| 45 | + |
| 46 | +Add the plugin to your `app.json` or `app.config.js`: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "expo": { |
| 51 | + "plugins": [ |
| 52 | + [ |
| 53 | + "rn-mlkit-ocr", |
| 54 | + { |
| 55 | + "ocrModels": ["latin", "chinese"], |
| 56 | + "ocrUseBundled": true |
| 57 | + } |
| 58 | + ] |
| 59 | + ] |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +#### For React Native CLI Projects |
| 65 | + |
| 66 | +##### Android |
| 67 | + |
| 68 | +Add the following to your `android/build.gradle` file inside the `buildscript { ext { ... } }` block: |
| 69 | + |
| 70 | +```gradle |
| 71 | +buildscript { |
| 72 | + ext { |
| 73 | + // ... other configurations |
| 74 | + ocrModels = ["latin", "chinese"] |
| 75 | + ocrUseBundled = true |
| 76 | + } |
| 77 | +} |
10 | 78 | ``` |
11 | 79 |
|
| 80 | +##### iOS |
| 81 | + |
| 82 | +Add the following to your `ios/Podfile` before the `use_react_native!` call: |
| 83 | + |
| 84 | +```ruby |
| 85 | +# --- RN-MLKIT-OCR CONFIG --- |
| 86 | +$ReactNativeOcrSubspecs = ['Latin', 'Chinese'] |
| 87 | +# --- END RN-MLKIT-OCR CONFIG --- |
| 88 | +``` |
| 89 | + |
| 90 | +### Configuration Options |
| 91 | + |
| 92 | +- **`ocrModels`**: Array of language models to include |
| 93 | + - Available options: `'latin'`, `'chinese'`, `'devanagari'`, `'japanese'`, `'korean'`, or `'all'` |
| 94 | + - Default: `['all']` |
| 95 | +- **`ocrUseBundled`** (Android only): Whether to use bundled models |
| 96 | + - `true`: Models are bundled with the app (larger app size, works offline immediately) |
| 97 | + - `false`: Models are downloaded on first use (smaller app size, requires internet on first use) |
| 98 | + - Default: `false` |
12 | 99 |
|
13 | 100 | ## Usage |
14 | 101 |
|
| 102 | +### Basic Text Recognition |
| 103 | + |
| 104 | +```typescript |
| 105 | +import { recognizeText } from 'rn-mlkit-ocr'; |
| 106 | + |
| 107 | +const imageUri = 'file:///path/to/image.jpg'; // or 'https://...' |
| 108 | + |
| 109 | +try { |
| 110 | + const result = await recognizeText(imageUri); |
| 111 | + console.log('Recognized text:', result.text); |
| 112 | + |
| 113 | + // Access detailed information |
| 114 | + result.blocks.forEach((block) => { |
| 115 | + console.log('Block:', block.text); |
| 116 | + block.lines.forEach((line) => { |
| 117 | + console.log(' Line:', line.text); |
| 118 | + line.elements.forEach((element) => { |
| 119 | + console.log(' Element:', element.text); |
| 120 | + }); |
| 121 | + }); |
| 122 | + }); |
| 123 | +} catch (error) { |
| 124 | + console.error('OCR Error:', error); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### Using Specific Language Models |
15 | 129 |
|
16 | | -```js |
17 | | -import { multiply } from 'rn-mlkit-ocr'; |
| 130 | +```typescript |
| 131 | +import { recognizeText } from 'rn-mlkit-ocr'; |
18 | 132 |
|
19 | | -// ... |
| 133 | +// Recognize Chinese text |
| 134 | +const result = await recognizeText(imageUri, 'chinese'); |
20 | 135 |
|
21 | | -const result = multiply(3, 7); |
| 136 | +// Recognize Japanese text |
| 137 | +const result = await recognizeText(imageUri, 'japanese'); |
22 | 138 | ``` |
23 | 139 |
|
| 140 | +### Getting Available Languages |
24 | 141 |
|
25 | | -## Contributing |
| 142 | +```typescript |
| 143 | +import { getAvailableLanguages } from 'rn-mlkit-ocr'; |
| 144 | + |
| 145 | +const languages = await getAvailableLanguages(); |
| 146 | +console.log('Available languages:', languages); |
| 147 | +// Output: ['latin', 'chinese'] |
| 148 | +``` |
| 149 | + |
| 150 | +## API Reference |
| 151 | + |
| 152 | +### `recognizeText(imageUri: string, detectorType?: DetectorType): Promise<OcrResult>` |
| 153 | + |
| 154 | +Performs OCR on the specified image. |
| 155 | + |
| 156 | +**Parameters:** |
| 157 | + |
| 158 | +- `imageUri`: Path to the image (file path, content URI, or HTTP/HTTPS URL) |
| 159 | +- `detectorType`: Optional language detector type (`'latin'`, `'chinese'`, `'devanagari'`, `'japanese'`, `'korean'`). Defaults to `'latin'` |
| 160 | + |
| 161 | +**Returns:** Promise resolving to `OcrResult` |
| 162 | + |
| 163 | +### `getAvailableLanguages(): Promise<DetectorType[]>` |
| 164 | + |
| 165 | +Returns the list of language models available in the app based on your configuration. |
| 166 | + |
| 167 | +**Returns:** Promise resolving to array of detector types |
26 | 168 |
|
27 | | -- [Development workflow](CONTRIBUTING.md#development-workflow) |
28 | | -- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request) |
29 | | -- [Code of conduct](CODE_OF_CONDUCT.md) |
| 169 | +### Types |
30 | 170 |
|
31 | | -## License |
| 171 | +```typescript |
| 172 | +interface OcrResult { |
| 173 | + text: string; // Full recognized text |
| 174 | + blocks: OcrBlock[]; // Text blocks |
| 175 | +} |
32 | 176 |
|
33 | | -MIT |
| 177 | +interface OcrBlock { |
| 178 | + text: string; |
| 179 | + frame: OcrFrame; |
| 180 | + lines: OcrLine[]; |
| 181 | +} |
34 | 182 |
|
35 | | ---- |
| 183 | +interface OcrLine { |
| 184 | + text: string; |
| 185 | + frame: OcrFrame; |
| 186 | + elements: OcrElement[]; |
| 187 | +} |
| 188 | + |
| 189 | +interface OcrElement { |
| 190 | + text: string; |
| 191 | + frame: OcrFrame; |
| 192 | +} |
| 193 | + |
| 194 | +interface OcrFrame { |
| 195 | + x: number; |
| 196 | + y: number; |
| 197 | + width: number; |
| 198 | + height: number; |
| 199 | +} |
| 200 | + |
| 201 | +type DetectorType = 'latin' | 'chinese' | 'devanagari' | 'japanese' | 'korean'; |
| 202 | +``` |
| 203 | + |
| 204 | +## Supported Languages & Scripts |
| 205 | + |
| 206 | +For a complete list of supported languages, see [Google ML Kit Text Recognition Languages](https://developers.google.com/ml-kit/vision/text-recognition/v2/languages). |
| 207 | + |
| 208 | +## Example App |
| 209 | + |
| 210 | +Check out the example app in the `example/` directory for a complete working implementation. |
| 211 | + |
| 212 | +```bash |
| 213 | +cd example |
| 214 | +yarn install |
| 215 | + |
| 216 | +# For iOS |
| 217 | +cd ios && pod install && cd .. |
| 218 | +yarn ios |
| 219 | + |
| 220 | +# For Android |
| 221 | +yarn android |
| 222 | +``` |
| 223 | + |
| 224 | +## Contributing |
36 | 225 |
|
37 | | -Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) |
| 226 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments