flutter_ocr_identity_extractor 1.0.0
flutter_ocr_identity_extractor: ^1.0.0 copied to clipboard
A Flutter package for extracting text from images using Google ML Kit Text Recognition. Supports multiple scripts and provides a simple API for OCR operations.
Yousef Gamal Romieh #
Flutter Developer #
Flutter OCR Identity Extractor #
A Flutter package for extracting text from images using Google ML Kit Text Recognition. This package provides a simple and efficient API for OCR (Optical Character Recognition) operations.
Features #
- ✅ Extract text from images using Google ML Kit
- ✅ Support for multiple recognition scripts
- ✅ Combine results from different recognizers for better accuracy
- ✅ Simple and easy-to-use API
- ✅ Support for File, file path, and image bytes
- ✅ Singleton pattern for efficient resource management
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
flutter_ocr_identity_extractor: ^1.0.0
Then run:
flutter pub get
Platform Setup #
Android #
Add the following to your android/app/build.gradle:
android {
defaultConfig {
minSdkVersion 21
}
}
iOS #
Add the following to your ios/Podfile:
platform :ios, '10.0'
Then run:
cd ios && pod install
Usage #
Basic Usage #
import 'package:flutter_ocr_identity_extractor/flutter_ocr_identity_extractor.dart';
import 'dart:io';
// Extract text from image file
final imageFile = File('/path/to/your/image.jpg');
final text = await OcrService.instance.recognizeTextFromImage(imageFile);
if (text != null) {
print('Extracted text: $text');
} else {
print('No text found in image');
}
Extract from File Path #
final text = await OcrService.instance.recognizeTextFromPath('/path/to/image.jpg');
if (text != null) {
print('Extracted text: $text');
}
Extract from Image Bytes #
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
import 'package:flutter/material.dart';
final bytes = await imageFile.readAsBytes();
final inputImageData = InputImageMetadata(
size: Size(width, height),
rotation: InputImageRotation.rotation0deg,
format: InputImageFormat.nv21,
bytesPerRow: width,
);
final inputImage = InputImage.fromBytes(bytes: bytes, metadata: inputImageData);
final text = await OcrService.instance.recognizeTextFromBytes(inputImage);
Complete Example #
import 'package:flutter/material.dart';
import 'package:flutter_ocr_identity_extractor/flutter_ocr_identity_extractor.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
class OcrExample extends StatefulWidget {
@override
_OcrExampleState createState() => _OcrExampleState();
}
class _OcrExampleState extends State<OcrExample> {
String? _extractedText;
bool _isProcessing = false;
Future<void> _pickAndExtractText() async {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_isProcessing = true;
_extractedText = null;
});
final imageFile = File(pickedFile.path);
final text = await OcrService.instance.recognizeTextFromImage(imageFile);
setState(() {
_isProcessing = false;
_extractedText = text;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('OCR Text Extractor')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _isProcessing ? null : _pickAndExtractText,
child: Text('Pick Image and Extract Text'),
),
SizedBox(height: 20),
if (_isProcessing)
CircularProgressIndicator()
else if (_extractedText != null)
Padding(
padding: EdgeInsets.all(16),
child: Text(
_extractedText!,
style: TextStyle(fontSize: 16),
),
)
else
Text('No text extracted yet'),
],
),
),
);
}
@override
void dispose() {
OcrService.instance.dispose();
super.dispose();
}
}
API Reference #
OcrService #
Singleton service for OCR operations.
Methods
recognizeTextFromImage(File imageFile)
Extracts text from an image file.
- Parameters:
imageFile(File): The image file to process
- Returns:
Future<String?>- The extracted text, or null if no text is found
recognizeTextFromPath(String imagePath)
Extracts text from an image file path.
- Parameters:
imagePath(String): The path to the image file
- Returns:
Future<String?>- The extracted text, or null if no text is found
recognizeTextFromBytes(InputImage inputImage)
Extracts text from image bytes.
- Parameters:
inputImage(InputImage): The input image data
- Returns:
Future<String?>- The extracted text, or null if no text is found
dispose()
Disposes of the service resources. Call this when you're done using the service.
Requirements #
- Flutter SDK: >=3.0.0
- Dart SDK: >=3.0.0
- Android: minSdkVersion 21
- iOS: iOS 10.0+
Dependencies #
google_mlkit_text_recognition: ^0.12.0
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
Support #
If you encounter any issues or have questions, please open an issue on the GitHub repository.