processDocument method

Future<String> processDocument(
  1. String docImageFilePath
)

Executes character recognition on the given document image file and returns the detected text. This method emulates the behavior of the tesseract command line.

Implementation

Future<String> processDocument(String docImageFilePath) async {
  if (_needsInit) {
    _init();
  }

  final tempOutputFilePath = '$_tempDir/${uuid.v4()}';
  final file = File('$tempOutputFilePath.txt');

  final ffi.Pointer<ffi.Char> docImageFilePathPtr =
      docImageFilePath.toNativeUtf8().cast<ffi.Char>();
  final ffi.Pointer<ffi.Char> tempOutputFilePathPtr =
      tempOutputFilePath.toNativeUtf8().cast<ffi.Char>();

  try {
    bindings.flusseract.ProcessDocumentFile(
      handle,
      docImageFilePathPtr,
      tempOutputFilePathPtr,
    );

    if (!file.existsSync()) {
      throw TesseractInitException(
        'Failed to process document: $docImageFilePath',
      );
    }
    return file.readAsStringSync();
  } finally {
    calloc.free(tempOutputFilePathPtr);
    calloc.free(docImageFilePathPtr);

    if (file.existsSync()) {
      file.deleteSync();
    }
  }
}