flutter_native_ocr 0.1.0
flutter_native_ocr: ^0.1.0 copied to clipboard
A Flutter plugin for native OCR (Optical Character Recognition) using Apple's Vision framework on iOS and Google ML Kit on Android.
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_native_ocr/flutter_native_ocr.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _recognizedText = '';
bool _isLoading = false;
File? _imageFile;
final _nativeOcrPlugin = FlutterNativeOcr();
final ImagePicker _picker = ImagePicker();
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await _nativeOcrPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _pickImage() async {
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
_imageFile = File(image.path);
_recognizedText = '';
});
}
}
Future<void> _recognizeText() async {
if (_imageFile == null) return;
setState(() {
_isLoading = true;
});
try {
final text = await _nativeOcrPlugin.recognizeText(_imageFile!.path);
setState(() {
_recognizedText = text.isEmpty ? 'No text found in image' : text;
});
} catch (e) {
setState(() {
_recognizedText = 'Error: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Native OCR Example'),
backgroundColor: Colors.blue,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Running on: $_platformVersion',
style: Theme.of(context).textTheme.bodyMedium,
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: _pickImage,
icon: const Icon(Icons.photo_library),
label: const Text('Pick Image from Gallery'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 20),
if (_imageFile != null) ...[
Container(
height: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child: Image.file(
_imageFile!,
fit: BoxFit.contain,
),
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: _isLoading ? null : _recognizeText,
icon: _isLoading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.text_fields),
label: Text(_isLoading ? 'Recognizing...' : 'Recognize Text'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
),
],
const SizedBox(height: 20),
if (_recognizedText.isNotEmpty) ...[
const Text(
'Recognized Text:',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[100],
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child: Text(
_recognizedText,
style: const TextStyle(fontSize: 16),
),
),
],
],
),
),
),
);
}
}