Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dgerding opened this issue Jan 7, 2024 · 10 comments · May be fixed by #146
Closed

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

dgerding opened this issue Jan 7, 2024 · 10 comments · May be fixed by #146

Comments

@dgerding
Copy link

dgerding commented Jan 7, 2024

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
Copy link
Author

dgerding commented Jan 7, 2024

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
Copy link

sencagri commented Jan 8, 2024

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

@std66
Copy link

std66 commented Jan 8, 2024

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
Copy link
Contributor

paoldev commented Jan 8, 2024

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

@dgerding
Copy link
Author

dgerding commented Jan 9, 2024

@paoldev Pulling now and will report back. thanks.

@dgerding
Copy link
Author

@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
Copy link
Author

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
Copy link
Author

dgerding commented Jan 14, 2024

@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
Copy link
Contributor

paoldev commented Jan 14, 2024

@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
Copy link
Author

@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!

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants