tesseract_ocr 0.1.0 copy "tesseract_ocr: ^0.1.0" to clipboard
tesseract_ocr: ^0.1.0 copied to clipboard

outdated

Tesseract OCR

example/lib/main.dart

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;
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 = path.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),
            ],
          )),
    );
  }
}