尝试使用Firebase ml视觉读取图像上的文本时,应用程序崩溃

时间:2019-09-13 14:24:04

标签: android flutter firebase-mlkit

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

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)

我该如何解决?

2 个答案:

答案 0 :(得分:1)

我今天有同样的问题。我花了一些时间调试它,并找到了原因,希望它也能对您有所帮助。

我将3个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 (无此类文件或目录),为空)

然后我使用了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

Image Picker Library

Path Library

Path Provider Library

答案 1 :(得分: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,
);