Redth / ZXing.Net.Maui

Barcode Scanning for MAUI?
MIT License
460 stars 151 forks source link

How to Dispose of or Close Camera? (On Windows, the camera instance won't close for me) #159

Closed dgerding closed 8 months ago

dgerding commented 9 months ago

Hi All, Thanks to the contributors for a remarkable cross-platform tool. thanks!!

The library is working great for me on Android but on my Windows desktop, the scanner view won't close. By that I mean I can't put any other user views or pop up in front of it and it won't close from code behind. Tried all sorts of ways to stop camera, assume "BarcodeCameraView" in xaml for camera instance.

What's the correct, official way to tell the handler to turn off camera. I'm trying to do stuff after recognition in the same screen space but can't get the camera view (live video) to stop.

Thanks for any ideas.

Dave G

dgerding commented 9 months ago

What I tried from code behind to "deactivate" the camera view:

BarcodeCameraView.IsEnabled = false; BarcodeCameraView.IsVisible = false;

BarcodeCameraView.IsDetecting = false; BarcodeCameraView.CameraLocation = BarcodeCameraView.CameraLocation == CameraLocation.Rear ? CameraLocation.Front : CameraLocation.Rear; BarcodeCameraView.CameraLocation = CameraLocation.Rear; BarcodeCameraView.IsDetecting = false; BarcodeCameraView.IsEnabled = false; BarcodeCameraView.IsVisible = false;

BarcodeCameraView.IsVisible = false;

sencagri commented 9 months ago

Camera is not closing on iOS also. Not auto focusing either.

std66 commented 9 months ago

The proper way of "disposing" the camera on all platforms would be to use BarcodeCameraView.Handler.DisconnectHandler in the event Unloaded. However, it will throw an unhandled exception. This is a bug in the library.

paoldev commented 9 months ago

PR #146 should fix some unhandled exceptions when unloading a camera preview on Windows. Does it work for you?

dgerding commented 8 months ago

@paoldev Pulling now and will report back. thanks.

dgerding commented 8 months ago

@paoldev's PR #146 Is the best effort yet I've seen to get ZXing playing nice with AppShell.

In my own use ZXing.Maui I have taken to "swapping" the App.MainPage instance between the standard AppShell page stack and a standalone ContentPage to get ZXing to work on Android and Windows in my AppShell-based app. (No mac|ios hw to test on)

@paoldev's 146 is a better and likely requisite path for a Maui library, since AppShell is increasingly the norm for nav models. With #146 the camera view functions properly after the first view. Great!

If I navigate away and back (shell nav), however, the Control instance appears to have closed, showing an red X icon in place of the control instance. I'll investigate further as this may be intended behavior.

Thanks @paoldev

dgerding commented 8 months ago

I can only get the AppShell-friendly (#146) to work on "first page visit." I tried forcing a reinitialization of camera view in OnAppearing to fix nav away and back breaking camera function. No luck. Still getting the clearly intentional "missing control" icon when I return to the page.

dgerding commented 8 months ago

@paoldev Thanks again. Here's a screen showing the bug I'm encountering when running your 146 code with your AppShell demo. Each page with camera view works once. If I navigate back to any page with a camera view the second time, the _cameraPlaceholder (?) image seems to take over and the camera views won't return even using a new instance for the control set in OnAppearing.

I'm happy to try any tests or whatever. Any suggestions appreciated. I'll report back if I can figure out what's going on.

Do the controls works for you when exiting and returning via shell nav?

_cameraPlaceholder

paoldev commented 8 months ago

@dgerding The expected behaviour going back and forth between pages is that the camera stream should automatically (re)start when opening "MainPage" and "NewPage2".

I recompiled my demo app with latest VS 17.8.4 and, after fixing new unexpected compiler errors (due to wrong Windows target framework....) it still "seems" to work fine. Are you navigating through the menu or in a different way?

Are you using an USB camera? What happens if you physically detach and reattach it when the stream appears stopped?

However, I updated the demo app by adding a lot of debug messages. Can you verify if you receive CameraPreview_Loaded and CameraPreview_Unloaded messages every time you enter or exit a page? For example:

Entering the page the first time CameraManager 1 - ConnectAsync CameraManager 1 - UpdateCameraAsync "null" CameraManager 1 - CameraPreview_Loaded: no previous camera stream has been found CameraManager 1 - InitCameraUnlockedAsync: camera "WinDeviceId" correctly initialized. Watcher_Added DeviceId: "WinDeviceId" CameraManager 1 - UpdateCameraAsync "WinDeviceId" CameraManager 1 - InitCameraUnlockedAsync: camera "WinDeviceId" is already initialized. CameraManager 1 - InitCameraUnlockedAsync: camera "WinDeviceId" is already initialized.

Entering the page the second time and following CameraManager 1 - CameraPreview_Loaded: trying to initialize previous camera stream "WinDeviceId" CameraManager 1 - UpdateCameraAsync "WinDeviceId" CameraManager 1 - InitCameraUnlockedAsync: camera "WinDeviceId" correctly initialized.

Exiting the page CameraManager 1 - CameraPreview_Unloaded: saving camera stream "WinDeviceId" CameraManager 1 - CleanupCameraAsync "null" CameraManager 1 - UninitCameraUnlockedAsync: start releasing camera "WinDeviceId" CameraManager 1 - UninitCameraUnlockedAsync: camera released

dgerding commented 8 months ago

@paoldev I pulled, built and successfully ran your latest AppShell branch and it worked. This is great news for me as it means the bug is something in my implementation. Also: Your AppShell branch also works on Windows and Android when targeting .net8 for the app and ZXing.Net.Maui projects also targeting .net8. (Couldn't test Apple stuff) Again, great news.

I'm going work with your PR code as now and will close this issue. (I can now update my app from weird "swapping main page" workaround I'd implemented to get things working with AppShell properly)

Thanks again!

:)