4

我正在尝试使用 firebase ml vision 从图像中读取文本,并使用 image_picker 从图库中获取图像。

图像选择器:^0.6.1+4

firebase_ml_vision:^0.9.2+1

获取图像

File imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);

应用程序仅在执行以下代码的最后一行时才会崩溃

final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);

final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();

final VisionText visionText = await textRecognizer.processImage(visionImage);
//crash the app when execution above line

错误信息

W/ExifInterface(15273): Skip the tag entry since data format (ULONG) is unexpected for tag: LightSource
W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
W/ExifInterface(15273): Skip the tag entry since data format (ULONG) is unexpected for tag: LightSource
W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugins.firebasemlvision.FirebaseMlVisionPlugin.getDetector(FirebaseMlVisionPlugin.java:205)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugins.firebasemlvision.FirebaseMlVisionPlugin.handleDetection(FirebaseMlVisionPlugin.java:74)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugins.firebasemlvision.FirebaseMlVisionPlugin.onMethodCall(FirebaseMlVisionPlugin.java:49)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.app.ActivityThread.main(ActivityThread.java:6669)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/flutter (15273): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null)
E/flutter (15273): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (15273): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (15273): <asynchronous suspension>
E/flutter (15273): #2      TextRecognizer.processImage (package:firebase_ml_vision/src/text_recognizer.dart:22:38)
E/flutter (15273): <asynchronous suspension>
E/flutter (15273): #3      _ScanState._initializeVision (package:scanner/ui/scan.dart:65:56)
E/flutter (15273): <asynchronous suspension>
E/flutter (15273): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter (15273): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter (15273): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (15273): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter (15273): #8      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter (15273): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter (15273): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter (15273): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter (15273): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (15273): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (15273): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (15273): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (15273): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (15273): #17     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (15273): #18     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (15273): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (15273): #20     _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter (15273): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)

我该如何解决?

4

2 回答 2

1

我今天刚遇到同样的问题。我花了一点时间调试它并找到了原因,我希望这也能帮助你。

我将三个 Firebase 行包裹在 try/catch 中。

try {
  final FirebaseVisionImage visionImage =
  final TextRecognizer _recognizer = FirebaseVision.instance.textRecognizer();
  final VisionText readText = await _recognizer.processImage(visionImage);
} catch(error) {
  print('processText() - Error: $error');
}

然后打印以下错误

processText() - 错误:PlatformException(MLVisionDetectorIOError, path_to_my_image (No such file or directory), null)

然后我使用了 Android Studios 设备文件资源管理器并查看了我的设备,果然,该文件不存在!

我最终创建了一个辅助函数来捕获图像,然后创建图像的副本(这解决了问题)。

import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart' as syspaths;
import 'package:path/path.dart' as path;

Future<String> takePicture() async {
  final imageFile = await ImagePicker.pickImage(
    source: ImageSource.camera,
  );

  if (imageFile == null) {
    return null;
  }

  final appDir = await syspaths.getApplicationDocumentsDirectory();
  final fileName = path.basename(imageFile.path);

  final savedImage = await imageFile.copy('${appDir.path}/$fileName'); //This is the line that fixed it for me
  return '${appDir.path}/$fileName';
}

如果你走这条路,你需要更新你的 pubspec.yaml 以包含以下依赖项。

image_picker: ^0.6.5+2
path:
path_provider: ^1.6.7

图像选择器库

路径库

路径提供程序库

于 2020-05-05T16:58:01.253 回答
0

我有同样的问题

  image_picker: ^0.6.1+4
  firebase_ml_vision: ^0.9.2+1

同样的故障点 - 在执行下面的最后一行时,但仅在 Android 上 - 在 IOS 上按预期工作。

final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(image);
final TextRecognizer textRecognizer =
    FirebaseVision.instance.textRecognizer();

//TODO: Failing on this line on Android only...
final VisionText visionText = await textRecognizer.processImage(
  visionImage,
);
于 2019-09-16T16:56:54.100 回答