ocr_flutter_web 1.0.1
ocr_flutter_web: ^1.0.1 copied to clipboard
A Flutter web plugin to extract text from images and PDF files using Tesseract.js and PDF.js. Supports English and Hindi with auto-rotation.
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:ocr_flutter_web/ocr_flutter_web.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _ocrFlutterWebPlugin = OcrFlutterWeb();
String _extractedText = '';
bool _isLoading = false;
Future<void> _pickFile() async {
try {
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['jpg', 'png', 'jpeg', 'pdf'],
withData: true,
);
if (result != null && result.files.single.bytes != null) {
setState(() {
_isLoading = true;
_extractedText = '';
});
final file = result.files.single;
final text = await _ocrFlutterWebPlugin.extractText(
file.bytes!,
filename: file.name,
// language: 'eng+hin',
language: 'eng',
);
print('extracted text: $text');
setState(() {
_extractedText = text;
_isLoading = false;
});
}
} catch (e) {
setState(() {
_extractedText = 'Error executing OCR: $e';
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('OCR Web Plugin Demo'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _isLoading ? null : _pickFile,
child: const Text('Upload Image or PDF'),
),
const SizedBox(height: 20),
if (_isLoading) const CircularProgressIndicator(),
if (!_isLoading && _extractedText.isNotEmpty)
Expanded(
child: SingleChildScrollView(
child: SelectableText(
_extractedText,
style: const TextStyle(fontSize: 16),
),
),
),
],
),
),
),
),
);
}
}