我正在尝试使用MethodChannel 在 Flutter 中使用bytedeco tesseract java 包装器,但是我遇到了错误。我已经尝试过 flutter clean 并将 gradle 升级到 4.2.2 但仍然无法正常工作。我的怀疑是正确安装依赖项
我通过 Maven 添加的依赖项如下:
android/app/build.gradle
dependencies {
implementation 'org.bytedeco:javacpp:1.5.6'
implementation 'org.bytedeco:javacpp-presets:1.5.6'
implementation 'org.bytedeco:leptonica:1.81.1-1.5.6'
implementation 'org.bytedeco:tesseract:4.1.1-1.5.6'
}
android/app/src/main/java/com/example/ocr_app_test_v2/MainActivity.java
package com.example.ocr_app_test_v2;
import io.flutter.embedding.android.FlutterActivity;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import org.bytedeco.javacpp.*;
import org.bytedeco.leptonica.*;
import org.bytedeco.tesseract.*;
import static org.bytedeco.leptonica.global.lept.*;
import static org.bytedeco.tesseract.global.tesseract.*;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "samples.flutter.dev/ocr";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
// Note: this method is invoked on the main thread.
// Note: this method is invoked on the main thread.
if (call.method.equals("getOCR_result")) {
String batteryLevel = getOCR_result();
result.success(batteryLevel);
} else {
result.notImplemented();
}
}
);
}
public String getOCR_result() {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(null, "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
// Open input image with leptonica library
PIX image = pixRead("/Users/nameHere/AndroidStudioProjects/ocr_app_test_v2/src_ocr/test_pic.png");
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
System.out.println("OCR output:\n" + outText.getString());
// Destroy used object and release memory
//api.End();
///outText.deallocate();
//pixDestroy(image);
return outText.getString();
}
}
我实现的飞镖方法:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Future<String> getOCR_result_dart() async {
const platform = MethodChannel('samples.flutter.dev/ocr');
try {
final String result = await platform.invokeMethod('getOCR_result');
print('resutls are here: ' + result);
return result;
} on PlatformException catch (e) {
return "ocr error";
}
}
我得到的错误:
W/System.err( 1283): Warning: Version of org.bytedeco:leptonica could not be found.
E/AndroidRuntime( 1283): FATAL EXCEPTION: main
E/AndroidRuntime( 1283): Process: com.example.ocr_app_test_v2, PID: 1283
E/AndroidRuntime( 1283): java.lang.UnsatisfiedLinkError: dlopen failed: library "libjnilept.so" not found
E/AndroidRuntime( 1283): at java.lang.Runtime.loadLibrary0(Runtime.java:1087)
E/AndroidRuntime( 1283): at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
E/AndroidRuntime( 1283): at java.lang.System.loadLibrary(System.java:1664)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1738)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.load(Loader.java:1345)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.load(Loader.java:1157)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.load(Loader.java:1133)
E/AndroidRuntime( 1283): at org.bytedeco.leptonica.global.lept.<clinit>(lept.java:14)
E/AndroidRuntime( 1283): at java.lang.Class.classForName(Native Method)
E/AndroidRuntime( 1283): at java.lang.Class.forName(Class.java:454)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.load(Loader.java:1212)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.load(Loader.java:1157)
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.load(Loader.java:1133)
E/AndroidRuntime( 1283): at org.bytedeco.tesseract.TessBaseAPI.<clinit>(TessBaseAPI.java:26)
E/AndroidRuntime( 1283): at com.example.ocr_app_test_v2.MainActivity.getOCR_result(MainActivity.java:52)
E/AndroidRuntime( 1283): at com.example.ocr_app_test_v2.MainActivity.lambda$configureFlutterEngine$0$MainActivity(MainActivity.java:35)
E/AndroidRuntime( 1283): at com.example.ocr_app_test_v2.-$$Lambda$MainActivity$j1X8cjOgJ0mnzzaqcNKQtS4D12U.onMethodCall(Unknown Source:2)
E/AndroidRuntime( 1283): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/AndroidRuntime( 1283): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/AndroidRuntime( 1283): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:865)
E/AndroidRuntime( 1283): at android.os.MessageQueue.nativePollOnce(Native Method)
E/AndroidRuntime( 1283): at android.os.MessageQueue.next(MessageQueue.java:335)
E/AndroidRuntime( 1283): at android.os.Looper.loop(Looper.java:206)
E/AndroidRuntime( 1283): at android.app.ActivityThread.main(ActivityThread.java:8595)
E/AndroidRuntime( 1283): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 1283): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
E/AndroidRuntime( 1283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
E/AndroidRuntime( 1283): Caused by: java.lang.UnsatisfiedLinkError: Could not find jnilept in class, module, and library paths.
E/AndroidRuntime( 1283): at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1705)
E/AndroidRuntime( 1283): ... 23 more
I/Process ( 1283): Sending signal. PID: 1283 SIG: 9
Lost connection to device.