juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
744 stars 444 forks source link

How can I control when the scanner is active? #1004

Closed burekas7 closed 1 month ago

burekas7 commented 1 month ago

Hi,

I want to have control over when the scanner will start scanning and when it will stop. How can this be done?

Currently the scanner starts scanning immediately when the screen comes up.

burekas7 commented 1 month ago

It seems that the controller has that: controller.start(), controller.stop() I will check it.

navaronbracke commented 1 month ago

In the new beta release, you have full control over the scanner lifecycle. So this is already possible.

burekas7 commented 1 month ago

@navaronbracke Thanks. But if I get it right, the way to do that is what I mentioned, right? (start/stop)

navaronbracke commented 1 month ago

Yes, that is the way to use the API.

burekas7 commented 1 month ago

@navaronbracke

But when I call the stop() it turns off the camera.

My purpose is only stopping the scanning action, not the camera.

Actually, I need an option to close/start the Stream<BarcodeCapture>

navaronbracke commented 1 month ago

In that case, we should handle pausing and resuming the StreamSubscription in mobile_scanner.

navaronbracke commented 1 month ago

See https://github.com/juliansteenbakker/mobile_scanner/issues/1006

burekas7 commented 1 month ago

@navaronbracke Thanks.

Currently I implemented this local solution: It works. But yes, it could be nice to do this via controller or something like that.

  void startListenToScannerCapture() {
    streamSubscription = controller.barcodes.listen(scannerlistener);
  }

  void scannerlistener(BarcodeCapture barcodeCapture) async {
    Some actions
    .
    .
    .
    .
    await streamSubscription.cancel();
  }

  // And when button pressed
    onPressed: () {
      startListenToScannerCapture();
    },
burekas7 commented 1 month ago

@navaronbracke

There is something weird here, When I do: streamSubscription.pause() And I move the scanner on a barcode, it doesn't read it, which is ok. But when I streamSubscription.resume(), the stream listener callback is run with a scan value of the barcode while I was on pause. Is it a bug?

navaronbracke commented 1 month ago

That could definitely be a bug related to pause / resume of the subscription

burekas7 commented 1 month ago

@navaronbracke Ok thanks, I will open a new issue for that.

=> https://github.com/juliansteenbakker/mobile_scanner/issues/1008

navaronbracke commented 1 month ago

@burekas7 No need for that, I added a note in https://github.com/juliansteenbakker/mobile_scanner/issues/1006

burekas7 commented 1 month ago

@navaronbracke Ok, so I closed #1008 and also added it as a comment in #1006