edufolly / flutter_mobile_vision

Flutter implementation of Google Mobile Vision.
MIT License
455 stars 168 forks source link

OCR crashes when tapping return or when tapping on recognized text, even when configured properly #63

Closed MatijaNovosel closed 3 years ago

MatijaNovosel commented 3 years ago

Greetings, I've configured the package as it was intended, added the OCR activity and needed permissions, but when I tap on the scanning screen I am greeted by a crash of my entire app.

This is what the debug console has to say:

W/ActivityThread(29080): SCHED: com.example.finapp/io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity [87, r=750ms, a=110ms, w=26173ms]
D/Camera  (29080): app passed NULL surface
W/Activity(29080): Slow Operation: Activity com.example.finapp/io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity onPause took 294ms
D/AndroidRuntime(29080): Shutting down VM
E/AndroidRuntime(29080): FATAL EXCEPTION: main
E/AndroidRuntime(29080): Process: com.example.finapp, PID: 29080
E/AndroidRuntime(29080): java.lang.RuntimeException: Unable to destroy activity {com.example.finapp/io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Thread$State java.lang.Thread.getState()' on a null object reference
E/AndroidRuntime(29080):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5056)
E/AndroidRuntime(29080):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5085)
E/AndroidRuntime(29080):    at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
E/AndroidRuntime(29080):    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
E/AndroidRuntime(29080):    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
E/AndroidRuntime(29080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2047)
E/AndroidRuntime(29080):    at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(29080):    at android.os.Looper.loop(Looper.java:224)
E/AndroidRuntime(29080):    at android.app.ActivityThread.main(ActivityThread.java:7590)
E/AndroidRuntime(29080):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(29080):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
E/AndroidRuntime(29080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/AndroidRuntime(29080): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Thread$State java.lang.Thread.getState()' on a null object reference
E/AndroidRuntime(29080):    at io.github.edufolly.fluttermobilevision.ui.CameraSource$FrameProcessingRunnable.release(CameraSource.java:1142)
E/AndroidRuntime(29080):    at io.github.edufolly.fluttermobilevision.ui.CameraSource$FrameProcessingRunnable.access$900(CameraSource.java:1119)
E/AndroidRuntime(29080):    at io.github.edufolly.fluttermobilevision.ui.CameraSource.release(CameraSource.java:334)
E/AndroidRuntime(29080):    at io.github.edufolly.fluttermobilevision.ui.CameraSourcePreview.release(CameraSourcePreview.java:85)
E/AndroidRuntime(29080):    at io.github.edufolly.fluttermobilevision.util.AbstractCaptureActivity.onDestroy(AbstractCaptureActivity.java:124)
E/AndroidRuntime(29080):    at android.app.Activity.performDestroy(Activity.java:8180)
E/AndroidRuntime(29080):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1342)
E/AndroidRuntime(29080):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5041)
E/AndroidRuntime(29080):    ... 11 more
I/Process (29080): Sending signal. PID: 29080 SIG: 9
Lost connection to device.
Exited (sigterm)

Is there any way to fix this, I used the 0.1.4 and 0.1.3 versions, both failed. My code is as follows:

import 'package:flutter_mobile_vision/flutter_mobile_vision.dart';

Future<Null> _read() async {
  List<OcrText> texts = [];
  try {
    texts = await FlutterMobileVision.read(
      camera: FlutterMobileVision.CAMERA_BACK,
      waitTap: true,
      showText: true,
    );
  } on Exception {
    texts.add(
      OcrText('Failed to recognize text'),
    );
  }
}
<...>
@override
void initState() {
  super.initState();
  FlutterMobileVision.start();
}
<...>
floatingActionButton: FloatingActionButton.extended(
  onPressed: _read,
  icon: Icon(Icons.camera_alt_rounded),
  backgroundColor: Colors.orange,
  label: Text("Scan"),
),

And the AndroidManifest.xml in main is as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.finapp">
  <uses-feature android:name="android.hardware.camera" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.CAMERA" />
  <application android:label="finapp" android:icon="@mipmap/ic_launcher" android:usesCleartextTraffic="true">
    <activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
      <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" />
      <meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity" />
    <meta-data android:name="flutterEmbedding" android:value="2" />
  </application>
</manifest>
rizkiawanp commented 3 years ago

I have same issue

wei-ancoris commented 3 years ago

@rizkiawan-p @MatijaNovosel I had the same issue when I use this library in my project, however, when I run example project it was ok, so I made a few changes that match the example

After I made following changes, this error seems to be resolve, I copied following settings from example project in this repo. Add android.enableR8=true to gradle.properties

in build.gradle, use classpath 'com.android.tools.build:gradle:3.6.3'

and in app/build.gradle change the compileSdkVersion 29 and targetSdkVersion 29

MatijaNovosel commented 3 years ago

It works, although I wouldn't consider downgrading those versions a practical solution. Thank you very much regardless!