platform_text_recognition
On-device OCR for Flutter using platform-native engines:
- iOS: Apple Vision (
VNRecognizeTextRequest) — no ML Kit or third-party iOS OCR dependencies - Android: Google ML Kit Text Recognition
Why this package?
Unlike all-in-one ML Kit plugins, this package avoids shipping ML Kit on iOS. That keeps the iOS build lighter and sidesteps Swift Package Manager compatibility issues with ML Kit pods.
Platform support
| Platform | Engine | Min version |
|---|---|---|
| iOS | Apple Vision | iOS 13+ |
| Android | ML Kit | API 21+ |
Installation
dependencies:
platform_text_recognition: ^0.2.0
Usage
import 'package:platform_text_recognition/platform_text_recognition.dart';
final text = await PlatformTextRecognizer.instance.recognizeText(
'/absolute/path/to/image.jpg',
);
print(text);
Or use the top-level helper:
final text = await recognizeTextFromImage('/absolute/path/to/image.jpg');
Returns an empty string when no text is detected. Throws PlatformException when the file is missing or recognition fails.
Language and script support
Use TextRecognitionScript to pick the OCR model on Android. On iOS, when you omit explicit language hints, the script maps to default Vision language codes.
// Japanese text (Android: ML Kit Japanese; iOS: ja-JP hint)
final japanese = await PlatformTextRecognizer.instance.recognizeText(
imagePath,
script: TextRecognitionScript.japanese,
);
// Chinese text
final chinese = await PlatformTextRecognizer.instance.recognizeText(
imagePath,
script: TextRecognitionScript.chinese,
);
// Devanagari (Hindi, etc.) — Android only
final hindi = await PlatformTextRecognizer.instance.recognizeText(
imagePath,
script: TextRecognitionScript.devanagari,
);
On iOS you can also pass explicit BCP-47 language codes:
final french = await PlatformTextRecognizer.instance.recognizeText(
imagePath,
languages: ['fr-FR', 'en-US'],
);
When languages is omitted on iOS 16+, Vision auto-detects the language for Latin script.
Arabic is supported on iOS via language codes (ML Kit does not offer an Arabic script model on Android):
final arabic = await PlatformTextRecognizer.instance.recognizeText(
imagePath,
languages: ['ar-SA'], // ar-EG, ar-AE, etc.
);
| Script | Android | iOS |
|---|---|---|
latin |
ML Kit Latin (bundled) | Auto-detect (or explicit languages) |
chinese |
ML Kit Chinese (opt-in) | zh-Hans, zh-Hant |
japanese |
ML Kit Japanese (opt-in) | ja-JP |
korean |
ML Kit Korean (opt-in) | ko-KR |
devanagari |
ML Kit Devanagari (opt-in) | Not supported by Vision |
Android optional script models
v0.2.0+ ships only the Latin ML Kit model by default (~38 MB). Add dependencies for other scripts in android/app/build.gradle.kts:
dependencies {
// Required only for TextRecognitionScript.chinese
implementation("com.google.mlkit:text-recognition-chinese:16.0.1")
// Required only for TextRecognitionScript.devanagari
implementation("com.google.mlkit:text-recognition-devanagari:16.0.1")
// Required only for TextRecognitionScript.japanese
implementation("com.google.mlkit:text-recognition-japanese:16.0.1")
// Required only for TextRecognitionScript.korean
implementation("com.google.mlkit:text-recognition-korean:16.0.1")
}
Add only the scripts you need. Using a non-Latin script without its dependency throws PlatformException with code SCRIPT_NOT_AVAILABLE and instructions in the message.
Example
See the example/ app for an image picker + OCR demo with a script selector. The example opts in to all Android script models for testing.
cd example
flutter run
Limitations
- File paths must point to local image files (
jpg,png, etc.). languagesis ignored on Android; use TextRecognitionScript there.- Devanagari and Arabic OCR are not available on both platforms (Devanagari: Android only; Arabic: iOS only).
- Web, macOS, Windows, and Linux are not supported.
License
MIT — see LICENSE.