edufolly / flutter_mobile_vision

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

App Crashes on OCR #71

Open Yoda-Man opened 3 years ago

Yoda-Man commented 3 years ago

Future _read() async { List texts = []; try { texts = await FlutterMobileVision.read( waitTap: true, flash: _torchOcr, autoFocus: _autoFocusOcr, multiple: _multipleOcr, showText: _showTextOcr, preview: _previewOcr, camera: _ocrCamera, fps: 2.0, ); setState(() { currentLogItem.washOption = texts[0].value; }); } on Exception { texts.add(OcrText('Failed to recognize text')); } } }

Vylwyn commented 3 years ago

same issue

Yoda-Man commented 3 years ago

I resolved the issue by adding the OCR activity to my AndroidManifest.xml after other activity nodes

Yoda-Man commented 3 years ago

<activity android:name="io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity" />

Vylwyn commented 3 years ago

I already have it - however I still face the issue - java.lang.RuntimeException: Unable to destroy activity {{packagename}/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

akulkarni9 commented 3 years ago

Yes, even I've same issue...

E/AndroidRuntime(24295): 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(24295): at io.github.edufolly.fluttermobilevision.ui.CameraSource$FrameProcessingRunnable.release(CameraSource.java:1142) E/AndroidRuntime(24295): at io.github.edufolly.fluttermobilevision.ui.CameraSource$FrameProcessingRunnable.access$900(CameraSource.java:1119) E/AndroidRuntime(24295): at io.github.edufolly.fluttermobilevision.ui.CameraSource.release(CameraSource.java:334) E/AndroidRuntime(24295): at io.github.edufolly.fluttermobilevision.ui.CameraSourcePreview.release(CameraSourcePreview.java:85) E/AndroidRuntime(24295): at io.github.edufolly.fluttermobilevision.util.AbstractCaptureActivity.onDestroy(AbstractCaptureActivity.java:124) E/AndroidRuntime(24295): at android.app.Activity.performDestroy(Activity.java:8251) E/AndroidRuntime(24295): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1364) E/AndroidRuntime(24295): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5290)

This is my code:
`class OCRPage extends StatefulWidget { @override _OCRPageState createState() => _OCRPageState(); }

class _OCRPageState extends State { int _ocrCamera = FlutterMobileVision.CAMERA_BACK; String _text = "TEXT";

Future _read() async { List texts = []; try { texts = await FlutterMobileVision.read( camera: _ocrCamera, waitTap: true, );

  setState(() {
    _text = texts[0].value;
  });
} on Exception {
  texts.add(OcrText('Failed to recognize text'));
}

}

@override Widget build(BuildContext context) { // TODO: implement build return SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Lottie.asset('assets/scan-some-words.json', repeat: true), SizedBox(height: 20), Text( _text, style: GoogleFonts.orbitron( textStyle: TextStyle( color: Colors.white, fontSize: 20, fontWeight: FontWeight.w500)), textAlign: TextAlign.center, ), SizedBox(height: 10), Text( Morse(_text).encode(), style: GoogleFonts.orbitron( textStyle: TextStyle( color: Colors.white, fontSize: 20, fontWeight: FontWeight.w500)), textAlign: TextAlign.center, ), SizedBox(height: 30), ElevatedButton( onPressed: _read, child: new Text('Start Scanning'), // color: Color(0xFFFF16CD), // splashColor: Colors.orangeAccent, ), SizedBox(height: 10), Text( 'Tap on the text to convert into Morse Code', style: TextStyle(color: Color(0xffE6BBFC)), ) ])); } }`

Oranji-Aka commented 3 years ago

found in a other camera thread, it fixed this issue replace with this in CameraSource.java from the library.

this is my path but you can adjust for yours D:\flutter.pub-cache\hosted\pub.dartlang.org\flutter_mobile_vision-0.1.4+1\android\src\main\java\io\github\edufolly\fluttermobilevision\ui

@SuppressLint("Assert") private void release() { assert (processingThread == null || processingThread.getState() == State.TERMINATED); if (mDetector != null) { mDetector.release(); mDetector = null; } }

RohitLuthra19 commented 3 years ago

Same for me.

E/AndroidRuntime(21923): java.lang.RuntimeException: Unable to destroy activity {com.uhs.vehicollate2021/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(21923): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4521) E/AndroidRuntime(21923): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4539) E/AndroidRuntime(21923): at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:39) E/AndroidRuntime(21923): at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) E/AndroidRuntime(21923): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) E/AndroidRuntime(21923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) E/AndroidRuntime(21923): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(21923): at android.os.Looper.loop(Looper.java:201) E/AndroidRuntime(21923): at android.app.ActivityThread.main(ActivityThread.java:6810) E/AndroidRuntime(21923): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(21923): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) E/AndroidRuntime(21923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) E/AndroidRuntime(21923): 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(21923): at io.github.edufolly.fluttermobilevision.ui.CameraSource$FrameProcessingRunnable.release(CameraSource.java:1142) E/AndroidRuntime(21923): at io.github.edufolly.fluttermobilevision.ui.CameraSource$FrameProcessingRunnable.access$900(CameraSource.java:1119) E/AndroidRuntime(21923): at io.github.edufolly.fluttermobilevision.ui.CameraSource.release(CameraSource.java:334) E/AndroidRuntime(21923): at io.github.edufolly.fluttermobilevision.ui.CameraSourcePreview.release(CameraSourcePreview.java:85) E/AndroidRuntime(21923): at io.github.edufolly.fluttermobilevision.util.AbstractCaptureActivity.onDestroy(AbstractCaptureActivity.java:124) E/AndroidRuntime(21923): at android.app.Activity.performDestroy(Activity.java:7524) E/AndroidRuntime(21923): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1307) E/AndroidRuntime(21923): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4506) E/AndroidRuntime(21923): ... 11 more

Gustavoo44 commented 2 years ago

Same issue.. Does someone fixed that or know other way to do same OCR text recognition?

Thanks

Huntler commented 2 years ago

I got the same issue as well. Error message:

E/AndroidRuntime( 5573): java.lang.RuntimeException: Unable to destroy activity {com.example.receipt_scanner/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

Seems like a basic Nullpointer exception which causes unable to destroy activity {com.example.receipt_scanner/io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity}.

I think I located the cause at this position. Here, the code processingThread.getState() causes a Nullpointer exception because processingThread is a null object. In my opinion, the processingThread was released, before this method is executed.

rguerra712 commented 2 years ago

I have found that this bug looks to be tied to the gradle version.

When I upgrade my build.grade to classpath to version com.android.tools.build:gradle:4.1.0 or greater in conjunction to the gradle-wrapper.properties distribution to distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip or greater, this is what causes this library to crash.

I have been able to fix this problem by tying my app to older versions com.android.tools.build:gradle:3.5.0 and distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip respectively.

Although it is not ideal nor a good long-term solution, it at least is a temporary workaround I have found in case it helps anyone else.