boursorama / ocr_scan_text

OCR Flutter
MIT License
10 stars 8 forks source link

Flutter OCR Scan Text

OCR Flutter v1.3.1

Flutter OCR Scan Text is a wrapper around the "Google ML kit Text Recognition" library. It helps to facilitate accurate text search and display of results from the camera. It also allows to manage text searches from an image or a pdf.

Features

Allows you to easily scan text from the camera, extract accurate results and display them to the user.

The results are returned by list of Block.

Note: The library uses the Camera package, be sure to ask for permission.

Usage

Add package in pubspec.yaml :

dependencies:
ocr_scan_text: 1.3.1

To use the library, import :

import 'package:ocr_scan_text/ocr_scan_text.dart';

To display the text detection widget with camera:

 LiveScanWidget(ocrTextResult: (ocrTextResult) {}, scanModules: [ScanAllModule()],)

A LiveScanWidget needs a module list to start detection. Validated results will be returned to the "matchedResult" method.

Create a scan module :

In this example (see the /example folder), we consider all Elements to be results.

Create a scan module :

class ScanAllModule extends ScanModule

The constructor of a module:

The purpose of a module is to search among the Blocks for a list of results (ScanResult) and to return it using the "matchedResult" method.

@override
Future<List<ScanResult>> matchedResult(List<TextBlock> textBlock, String text) async {
 List<ScanResult> list = [];
 for (var block in textBlock) {
  for (var line in block.lines) {
   for (var element in line.elements) {
    list.add(ScanResult(cleanedText: element.text, scannedElementList: [element]));
   }
  }
 }
 return list;
}

Start a module :

module.start();

Stop a module :

module.stop();

Scan file with Widget (Supported extension : png, jpg and pdf )

StaticScanWidget(ocrTextResult: (ocrTextResult) {}, scanModules: [ScanAllModule()], file: File("path/image.png"));

Scan file without Widget (Supported extension : png, jpg and pdf )

This method open gallery for pick a pics and start text analyze. ( /!\ verify permissions before )

OcrScanService([module]).startScanWithPhoto();

This method open file folder for pick a file and start text analyze. ( /!\ verify permissions before )

OcrScanService([module]).startScanWithOpenFile();

This method start text analyze

OcrScanService([module]).startScanProcess(File('path/image.png'));

Helper

You can use TextBlockHelper methods to help find results.