recognizeImage method

  1. @override
Future<List<OcrResult>> recognizeImage(
  1. String imagePath, {
  2. OcrOptions? options,
})
override

从图片文件路径识别文字。

options 为空时使用引擎级默认选项(见 setDefaultOptions), 否则用给定选项跑一次多分支预处理识别。

Implementation

@override
Future<List<OcrResult>> recognizeImage(
  String imagePath, {
  OcrOptions? options,
}) async {
  final engine = _requiredEngine;
  final bindings = PpOcrFfiBindings.instance;
  final PpOcrResultArrayC resultC;
  if (options == null) {
    resultC = bindings.recognizeFile(engine, imagePath);
  } else {
    final optsPtr = options.toNativeStruct();
    try {
      resultC = bindings.recognizeFileEx(engine, imagePath, optsPtr);
    } finally {
      calloc.free(optsPtr);
    }
  }

  if (resultC.error != nullptr) {
    final err = resultC.error.toDartString();
    throw Exception('OCR recognition failed: $err');
  }

  return _parseResultsC(resultC);
}