jhoogstraat / fast_barcode_scanner

A flutter plugin that allows your users to scan any type of barcode on iOS and Android
40 stars 64 forks source link

DetectionMode.pauseDetection freeze the camera #3

Closed allanwolski closed 3 years ago

allanwolski commented 3 years ago

Good job, this plugin is very good.

I want to pause the detector when a barcode is detected, but without freeze the camera.

I tried with pauseDetection and pauseVideo modes, but both freeze the camera.

jhoogstraat commented 3 years ago

Hi, thanks for your input!

I will look into it.

My only concern would be that the user cannot know whether detection is enabled or not. At least not without additional ui.

jhoogstraat commented 3 years ago

I pushed a new version (1.1.1) which should fix this.

Please confirm, so I can close this issue.

allanwolski commented 3 years ago

Thanks for the quick fix.

DetectionMode.pauseDetection works correctly now, but the camera freezes a bit when we call CameraController.instance.resumeDetector().

Is there anything that can be done?

jhoogstraat commented 3 years ago

If you are talking about Android then it's because the camera is basically restarted from scratch. Every time the Camera is resumed all use cases are rebound and I don't know if we can avoid rebinding the preview...

allanwolski commented 3 years ago

I'm trying to change from fast_qr_reader_view to fast_barcode_scanner because it has been descontinued. The fast_qr_reader_view packeage does not freeze at scan interval.

https://github.com/code-heroes-pty-ltd/fast_qr_reader_view

jhoogstraat commented 3 years ago

Ah okay, great to hear!

I'll try the plugin and see what they are doing different. I am confident that we can achieve the same experience.

Edit: Isn't it the same as the continous mode? The only difference is that calls are throttled.

allanwolski commented 3 years ago

Thanks for the quick answer.

Yes, but I need to pause detection for a few seconds to prevent the same code from being read multiple times.

jhoogstraat commented 3 years ago

What you are describing is basically throttling. You can use somethink like this. I think I'll add some configurable throttling on the native side (in seconds).

Edit: Oh wait, you are right. pauseDetection should not freeze the screen. Will look into that.

jhoogstraat commented 3 years ago

Please check the latest commit on the dev branch (#17). It should work on Android, but I didn't touch the ios implementation, yet.

Basically, on Android, CameraController now has pause/resumeCamera() and pause/resumeScanner(). When using pauseDetector mode, you have to call resumeScanner() to resume actually scanning frames.

allanwolski commented 3 years ago

Great @jhoogstraat, it's worked.

I can't use CameraController.instance anymore? Do I need to update minSdkVersion to 23?

jhoogstraat commented 3 years ago

Yeah you can just use CameraController() now. It will return a singleton, still.

Yeah you need to updated to 23. That will allow a for a simplified permissions handling.

allanwolski commented 3 years ago

I thought it was because the camera2 API.

I have many users who still use API 21 and 22. Is it possible to support this level of API in version 2.0.0 of the package?

jhoogstraat commented 3 years ago

Wow, that's quite old software. I'll see what I can do.

allanwolski commented 3 years ago

Thanks man, your package is very good.

jhoogstraat commented 3 years ago

I think I was wrong about the minSdk requirement. It should be reverted on #17. I was right, Android Studio just didn't output the error. I will push an update. Oh and this plugin uses CameraX on Android, instead of Camera2.

allanwolski commented 3 years ago

Perfect.

I have an error when I go back to the scanner, even calling CameraController().dispose().

I/flutter (21449): Error reading from camera: PlatformException(NOT_RUNNING, Camera is not running, null, null)
I/flutter (21449): Error reading from camera: PlatformException(ALREADY_RUNNING, Camera is already running, null, null)
jhoogstraat commented 3 years ago

That's weird. Android or iOS? You don't need to call dispose if the user is able to pull up the scanner it any time. You can just pause it and keep everything in memory.

The sample app has a configuration screen, which pauses the scanner when it is shown.

allanwolski commented 3 years ago

I'm testing on Android.

When closing the scanner screen, the log below is displayed on the console.

D/UseCaseAttachState(23127): Active and attached use case: [] for camera: 0
D/DeferrableSurface(23127): use count-1,  useCount=0 closed=true androidx.camera.core.impl.ImmediateSurface@ec6c6c2
D/DeferrableSurface(23127): Surface no longer in use[total_surfaces=2, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@ec6c6c2}
D/DeferrableSurface(23127): Surface terminated[total_surfaces=1, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@ec6c6c2}
D/DeferrableSurface(23127): use count-1,  useCount=0 closed=true androidx.camera.core.SurfaceRequest$2@e834437
D/DeferrableSurface(23127): Surface no longer in use[total_surfaces=1, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@e834437}
D/DeferrableSurface(23127): Surface terminated[total_surfaces=0, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@e834437}
D/Camera2CameraImpl(23127): {Camera@8b1a575[id=0]} CameraDevice.onClosed()
D/Camera2CameraImpl(23127): {Camera@8b1a575[id=0]} Transitioning camera internal state: CLOSING --> INITIALIZED
D/CameraStateRegistry(23127): Recalculating open cameras:
D/CameraStateRegistry(23127): Camera                                       State
D/CameraStateRegistry(23127): -------------------------------------------------------------------
D/CameraStateRegistry(23127): Camera@8b1a575[id=0]                         CLOSED
D/CameraStateRegistry(23127): Camera@5afce57[id=1]                         UNKNOWN
D/CameraStateRegistry(23127): -------------------------------------------------------------------
D/CameraStateRegistry(23127): Open count: 0 (Max allowed: 1)
D/CameraStateMachine(23127): New public camera state CameraState{type=CLOSED, error=null} from CLOSED and null
D/CameraStateMachine(23127): Publishing new public camera state CameraState{type=CLOSED, error=null}
jhoogstraat commented 3 years ago

This looks normal. How does you dart code look like?

You have to call start/pauseCamera and not pause/start Scanner if you need to restart the whole camera (on #17).

allanwolski commented 3 years ago

When navigating to the scanner screen for the first time it works, but when going back to home and navigating to the scanner screen again it shows the error.

jhoogstraat commented 3 years ago

Check out the latest commit. Is your crash gone? It replicated your navigation path in the example app and it does not crash anymore.

allanwolski commented 3 years ago

It worked, thanks a lot.

When do you plan to publish this version?

guilhermedaldim commented 3 years ago

@jhoogstraat great job, your package is good!

I'm waiting for this too, when do you intend to publish this version?

jhoogstraat commented 3 years ago

I am currently overthinking CameraController and writing tests for all classes and behaviours. I'd be great if you used (aka test) the Plugin intensively.

jhoogstraat commented 3 years ago

What would you like to see in the plugin? What don't you need?