afriscic / BarcodeScanning.Native.Maui

Barcode scanning library for .NET MAUI
https://www.nuget.org/packages/BarcodeScanning.Native.Maui
MIT License
162 stars 31 forks source link

AimMode causes camera to disconnect. #53

Closed MaximG1234 closed 4 months ago

MaximG1234 commented 4 months ago

Firstly, I can't thank you enough for this code. Brilliant work!

There does however appear to be a relatively minor issue with the library (V1.4.3) if I turn on AimMode=True.

My camera view starts freezing every 10 or so seconds and if I look at the logs I can see the following each time the camera freezes. The application does right itself and it does continue scanning but there is an obvious moment where everything stops working.

This isn't a huge issue for me and if I get a chance I will see if I can resolve it myself but just figured I would report it.

Thanks again for all your effort!

log.txt

[CameraManagerGlobal] Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_CLOSED for client com.mobile.test API Level 2 User Id 0
[Camera2CameraImpl] {Camera@2b275d5[id=0]} Resetting Capture Session
[Camera2CameraImpl] {Camera@2b275d5[id=0]} Releasing session in state CLOSING
[Camera2CameraImpl] {Camera@2b275d5[id=0]} Transitioning camera internal state: CLOSING --> REOPENING
[CameraStateRegistry] Recalculating open cameras:
[CameraStateRegistry] Camera                                       State                 
[CameraStateRegistry] -------------------------------------------------------------------
[CameraStateRegistry] Camera@476bf24[id=1]                         UNKNOWN               
[CameraStateRegistry] Camera@55c4289[id=2]                         UNKNOWN               
[CameraStateRegistry] Camera@2b275d5[id=0]                         OPENING               
[CameraStateRegistry] Camera@285f99a[id=3]                         UNKNOWN               
[CameraStateRegistry] -------------------------------------------------------------------
[CameraStateRegistry] Open count: 1 (Max allowed: 1)
[CameraStateMachine] New public camera state CameraState{type=OPENING, error=null} from OPENING and null
[CameraStateMachine] Publishing new public camera state CameraState{type=OPENING, error=null}
MaximG1234 commented 4 months ago

Hmmm, ok strange. I am now starting to see this happen with AimMode=False too. Could it be my device? Is anyone else seeing this?

nzohrab commented 4 months ago

Hi There, Do you know whether you're getting the same issue with previous versions of the library?

MaximG1234 commented 4 months ago

Hey @nzohrab I havent had a chance to properly test this (including in release mode) to see if this is a new issue. I will post when I have more info.

afriscic commented 4 months ago

Hello. Can you please share what device and Android version are you using? It got reported that some devices are more stable than others.

I have new release planned that should probably fix at least some of this camera resetting issues. It turns out that all of the async code messes with code execution order so that ImageProxy gets disposed multiple times. That throws an ObjectDisposedException which resets the camera by design. Although it is against the common practice, converting the detection code to sync fixes the issue, and I don't think it will be a problem as the detection code gets run on a background thread anyway. I just wanted to squeeze in a new feature to save an image from the camera so it'll take a few more days.

MaximG1234 commented 4 months ago

Hey @afriscic,

Thanks for the response.

I am using a Samsung A54 running Android 14. I can confirm this issue is occuring with AimMode turned on or off but I also don't believe the issue is happening in release compilation. While I haven't 100% confirmed this, there is usually a screen freeze while the camera reconnects (in debug) which does not appear to occur when in release.

I can also confirm that I have gone back as far as 2-3 versions of the library and it also seems to exist in those versions also.

I will try to provide more information when I have time but given it doesn't appear to be happening in release its probably a somewhat low priority issue.

MaximG1234 commented 4 months ago

As you suspected your latest update @afriscic appears to have fixed this problem. I will keep this task open for a few more days to confirm that the problem doesn't come back but almost certain this is resolved.

Thank you!