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
756 stars 446 forks source link

Set "zoomScale" when Initializing the MobileScannerController #576

Open jakobhec opened 1 year ago

jakobhec commented 1 year ago

It would be nice to be able to specify an initial zoom like this:

controller = MobileScannerController(initialZoomScale: 0.5);

Currently, the initial zoomScale is 0.0 and our users need to zoom in manually almost every time because their QR codes are too small.

berdibekovaG commented 1 year ago

it would be great !

joo commented 1 year ago

I agree, that would be great!

iulianxpopa commented 1 year ago

Have you found a workaround for setting an initial zoom scale?

jakobhec commented 1 year ago

I couldn't find one.

rvonkriegenbergh commented 1 year ago

I agree, I would use that too!

1nhale commented 1 year ago

so any success in finding a solution for this?

burekas7 commented 1 year ago

You have this function in the Controller: setZoomScale(zoomScale)

berdibekovaG commented 1 year ago

yes I have but it doesn't work if you want to set initial zoom

burekas7 commented 1 year ago

@berdibekovaG What is the difference between setting it as initial value or in the initState right after creating the instance of the controller? (But I agree that it could be nice to add this option as initial value)

mixable commented 1 year ago

When using setZoomScale, there is a zoom "animation" from 0 to the zoomScale value. This is fine for changing the zoom on a later state. But when looking at the image for the first time, it's difficult to realize what's going on and it takes some time to get the current image and the camera position when the zoom automatically changes. Having a fixed initial zoom value (without animation) it's much easier to get the initial state when looking at the image.

joo commented 11 months ago

my solution: on scanner started, set zoom scale. the effect is equivalent to: Set "zoomScale" when Initializing the MobileScannerController

MobileScanner(
    controller: scannerController,
    fit: BoxFit.contain,
    onDetect: onDetect,
    onScannerStarted: (_) {
      scannerController.setZoomScale(0.8);
    },
  )
burekas7 commented 11 months ago

@joo On Android it opens the camera after the zoom has done, But on iOS there is annoying zoom animation.

Is there any other way to solve this?

joo commented 11 months ago

@burekas7 , I am not test on ios, wait me.

joo commented 11 months ago

@burekas7 , Maybe you can setZoomScale delay by 50 milliseconds. Remember to upgrade MobileScanner to 3.0

MobileScanner(
    controller: scannerController,
    fit: BoxFit.contain,
    onDetect: onDetect,
    onScannerStarted: (_) {
      Future.delayed(const Duration(milliseconds: 50), (){
       scannerController.setZoomScale(0.8);
            });
    },
  )
mixable commented 11 months ago

@joo As @burekas7 mentioned, there is always a zoom animation when you use setZoomScale() (with or without delay). iOS has the following behavior: calling setZoomScale() before the scanner started, the zoom value is ignored - calling setZoomScale() after the scanner started, the zoom value is set with an animation.

The idea of a initialZoomScale is to have the zoom already set before the scanner is started. This is not possible with setZoomScale() - at least not on iOS.

burekas7 commented 11 months ago

@burekas7 , Maybe you can setZoomScale delay by 50 milliseconds. Remember to upgrade MobileScanner to 3.0

MobileScanner(
    controller: scannerController,
    fit: BoxFit.contain,
    onDetect: onDetect,
    onScannerStarted: (_) {
      Future.delayed(const Duration(milliseconds: 50), (){
       scannerController.setZoomScale(0.8);
            });
    },
  )

Is anyone here have a strange bug that on some iPhones this solution doesn't work? It somehow back very quick to zoom scale of 0 or even doesn't run it.

mixable commented 11 months ago

I think the duration of 50ms is too short. Maybe the camera is "not started yet". For me, a larger duration works.

burekas7 commented 11 months ago

@mixable What is the duration you set?

burekas7 commented 11 months ago

As I see, it seems that there is kind of problem with new models of iPhones when using the onScannerStarted callback, I also try to increase the delay duration to 150 mSec but it didn't help, I also don't want to put a big duration there because it will be annoying for users this delay and the zoom animation on each scan.

Is anyone here have the same issue?

@juliansteenbakker

jakobhec commented 11 months ago

@burekas7 Yes, same issue for me

mixable commented 11 months ago

@burekas7 I used a duration of 1 second. But only to make sure that it really works. But as you mentioned, it would be better to have a smaller value (or even zero) to remove the delay and the zoom animation.

And this is the reason to have a parameter like "initialZoomScale" ;)

burekas7 commented 11 months ago

@mixable I put some values, from 450 mSec it starts to be ok, so I put 500 mSec for now. In Android by the way there is no zoom animation, it shows black screen instead during this delay.

So I totally agree with this request of "initialZoomScale".

burekas7 commented 11 months ago

@juliansteenbakker

Update: With version 3.3.0 it seems to work also without any delay. But there is a strange jumping in the camera when between zoom scales with big different (iPhones) It will be better if the initial zoom scale can be also configured within the controller before the camera actually show up.

rvonkriegenbergh commented 11 months ago

@juliansteenbakker @burekas7

I see the same behavior with 3.3.0. No delay needed but some kind of jumping in the camera. (iOS on a iPhone 12 mini)