processImage method

Future<OcrTextRecognizerResult?> processImage(
  1. InputImage inputImage,
  2. Size imageSize,
  3. Image? background,
  4. Mode mode,
  5. List<ScanModule> scanModules,
  6. TextRecognizer? recognizer,
)

Launch the search for results from the image for all the modules started

Implementation

Future<OcrTextRecognizerResult?> processImage(
  ml_kit.InputImage inputImage,
  Size imageSize,
  ui.Image? background,
  Mode mode,
  List<ScanModule> scanModules,
  ml_kit.TextRecognizer? recognizer,
) async {
  _actualMode = mode;
  ml_kit.TextRecognizer textRecognizer =
      recognizer ?? ml_kit.TextRecognizer();

  /// Ask MLKit to return the list of TextBlocks in the image
  final recognizedText = await textRecognizer.processImage(inputImage);

  /// create a global String corresponding to the texts found by MLKIt
  String scannedText = '';
  List<ml_kit.TextElement> textBlocks = [];
  for (final textBlock in recognizedText.blocks) {
    for (final element in textBlock.lines) {
      for (final textBlock in element.elements) {
        textBlocks.add(textBlock);
        scannedText += " ${textBlock.text}";
      }
    }
  }

  /// Start the text search for each module
  Map<ScanModule, List<ScanMatchCounter>> mapModule =
      <ScanModule, List<ScanMatchCounter>>{};
  for (var scanModule in scanModules) {
    if (!scanModule.started) {
      continue;
    }

    /// Generate the results of each module
    List<ScanMatchCounter> scanLines = await scanModule.generateResult(
      recognizedText.blocks,
      scannedText,
      imageSize,
    );

    mapModule.putIfAbsent(
      scanModule,
      () => scanLines,
    );
  }

  /// Create a ScanRenderer to display the visual rendering of the results found
  var painter = ScanRenderer(
    mapScanModules: mapModule,
    imageRotation: inputImage.metadata?.rotation ??
        ml_kit.InputImageRotation.rotation90deg,
    imageSize: imageSize,
    background: background,
  );

  Map<ScanModule, List<ScanResult>> mapResult =
      <ScanModule, List<ScanResult>>{};
  mapModule.forEach((key, matchCounterList) {
    List<ScanResult> list = matchCounterList
        .where(
          (matchCounter) => matchCounter.validated == true,
        )
        .map<ScanResult>((e) => e.scanResult)
        .toList();

    if (list.isNotEmpty) {
      mapResult.putIfAbsent(key, () => list);
    }
  });

  await textRecognizer.close();
  if (mapResult.isEmpty) {
    return null;
  }

  return OcrTextRecognizerResult(
    CustomPaint(
      painter: painter,
    ),
    mapResult,
  );
}