capacitor-community / barcode-scanner

A fast and efficient (QR) barcode scanner for Capacitor
MIT License
437 stars 169 forks source link

[ml-kit] App crashes after second scan #195

Closed Jopie01 closed 1 year ago

Jopie01 commented 1 year ago

Describe the bug I'm testing out the ml-kit branch to see how fast it is compared with the current scanner and I'm impressed how well it works. I mostly wanted to scan the DataMatrix 2D barcode and it doesn't matter how I hold the camera, it will be detected fast.

However, I had some trouble to get going because documentation wasn't up to date, but I got it working eventually. But then I ran into a fatal error in Android Studio when running the app on my phone:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 10322
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.getcapacitor.PluginCall.resolve(com.getcapacitor.JSObject)' on a null object reference
        at com.getcapacitor.community.barcodescanner.CapacitorCommunityBarcodeScanner$3.onSuccess(CapacitorCommunityBarcodeScanner.java:339)
        at com.getcapacitor.community.barcodescanner.CapacitorCommunityBarcodeScanner$3.onSuccess(CapacitorCommunityBarcodeScanner.java:304)
        at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.2:1)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6923)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

This error occurs when I want to start scanning for the second time.

To Reproduce The app is an extreme basic app build on top of EmberJS and using ember-cli-capacitor to get everything in shape. But I suspect you can convert this into plain Javascript.

The code:

// controllers/application.js
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { BarcodeScanner } from '@capacitor-community/barcode-scanner';

export default class ApplicationController extends Controller {
  @tracked foundCode = '';

  @action
  async callback(result) {  // called when scanner has something to show
    console.log("Got something back", result);
    if (result.hasContent) {
      console.log(result.content);
      this.foundCode = result.content;
      await BarcodeScanner.stop();
    }
  }

  @action
  async startScan() {   // called when the start button is pressed
    console.log("Start Scanning");
    await BarcodeScanner.requestPermissions();
    BarcodeScanner.hideBackground();
    BarcodeScanner.start({}, this.callback);
  }

  @action
  async stopScan() {  // called when the stop button is pressed
    console.log("Stop Scanning");
    BarcodeScanner.stop();
  }
}
// templates/application.hbs
Code = {{this.foundCode}}
<br/>
<button type="button" {{on 'click' this.startScan}}>Start</button>
<button type="button" {{on 'click' this.stopScan}}>Stop</button>

So when you press the Start button, the camera shows up and you can start scanning. After a barcode is scanned, the result is returned to the app using the callback function. The camera is then switched off and the app shows the scanned result.

Press the Start button again and scan another or the same barcode. When the barcode is scanned the app crashes with the fatal error shown above.

Expected behavior We should be able to scan multiple times without crashing.

Smartphone (please complete the following information):

Additional context I think I'm missing something here which is letting the app crash.

thegnuu commented 1 year ago

Thanks for checking this out, I was only working on the iOS implementation and have not tested the Android implementation fully, but I will have a look at this one. I am not really sure what exactly is happening here, it looks like some references are destroyed to early or something...

The docs will be updated once the new version is ready :)

Jopie01 commented 1 year ago

Thanks for looking into it. I also did some digging and when I moved the line https://github.com/capacitor-community/barcode-scanner/blob/ml-kit/android/src/main/java/com/getcapacitor/community/barcodescanner/CapacitorCommunityBarcodeScanner.java#L214 to line 225 I was able to scan multiple times without crashing.

So it seems that there is a collision in the scan results.

JanMisker commented 1 year ago

@Jopie01 @thegnuu Is this still an issue? I'm also testing with the ml-kit branch but I don't have any crashes.

Jopie01 commented 1 year ago

@JanMisker, I had this crash when the scanner detected a barcode and called my callback function. In that function I stop the scanner and the next time I start scanning and detect a barcode the app crashes. With continuous scanning I hadn't any problem.

JanMisker commented 1 year ago

@Jopie01 check; I use continues scanning only, and then pause while processing and showing feedback to the user. Note that on Android the pause() didn't do anything, but a PR that fixes that was just merged an hour ago.

thegnuu commented 1 year ago

Should be fixed :)