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.
example/lib/main.dart
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';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OCR Identity Extractor Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const OcrExamplePage(),
);
}
}
class OcrExamplePage extends StatefulWidget {
const OcrExamplePage({super.key});
@override
State<OcrExamplePage> createState() => _OcrExamplePageState();
}
class _OcrExamplePageState extends State<OcrExamplePage> {
String? _extractedText;
bool _isProcessing = false;
File? _selectedImage;
Future<void> _pickAndExtractText() async {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_isProcessing = true;
_extractedText = null;
_selectedImage = File(pickedFile.path);
});
try {
final text = await OcrService.instance.recognizeTextFromImage(
_selectedImage!,
);
setState(() {
_isProcessing = false;
_extractedText = text;
});
if (text == null || text.isEmpty) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('No text found in the image'),
),
);
}
}
} catch (e) {
setState(() {
_isProcessing = false;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error: $e'),
),
);
}
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('OCR Identity Extractor'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton.icon(
onPressed: _isProcessing ? null : _pickAndExtractText,
icon: const Icon(Icons.image),
label: const Text('Pick Image and Extract Text'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 20),
if (_selectedImage != null) ...[
Container(
height: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
_selectedImage!,
fit: BoxFit.contain,
),
),
),
const SizedBox(height: 20),
],
if (_isProcessing)
const Center(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Processing image...'),
],
),
),
)
else if (_extractedText != null && _extractedText!.isNotEmpty)
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Extracted Text:',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
SelectableText(
_extractedText!,
style: const TextStyle(fontSize: 16),
),
],
),
),
)
else if (_selectedImage != null)
const Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Text('No text found in the image'),
),
)
else
const Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Text('Please select an image to extract text'),
),
),
],
),
),
);
}
@override
void dispose() {
OcrService.instance.dispose();
super.dispose();
}
}