tesseract_ocr 0.2.1 tesseract_ocr: ^0.2.1 copied to clipboard
Tesseract 4 adds a new neural net (LSTM) based OCR engine which is focused on line recognition. It has unicode (UTF-8) support, and can recognize more than 100 languages.
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:tesseract_ocr/tesseract_ocr.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _extractText = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String extractText;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
final Directory directory = await getTemporaryDirectory();
final String imagePath = join(
directory.path,
"tmp_1.jpg",
);
final ByteData data = await rootBundle.load('packages/tesseract_ocr/images/test_16.jpg');
final Uint8List bytes = data.buffer.asUint8List(
data.offsetInBytes,
data.lengthInBytes,
);
await File(imagePath).writeAsBytes(bytes);
extractText = await TesseractOcr.extractText(imagePath);
} on PlatformException {
extractText = 'Failed to extract text';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_extractText = extractText;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Tesseract OCR'),
),
body: Column(
children: <Widget>[
Center(
child: Text('Detected Text: $_extractText\n'),
),
Image.asset('images/test_16.jpg',package: 'tesseract_ocr', height: 30.0),
],
)),
);
}
}