Redth / ZXing.Net.Maui

Barcode Scanning for MAUI?
MIT License
437 stars 146 forks source link

How to stop BarcodeReaderView or release the camera resource.? #118

Open JasonYangNWI opened 1 year ago

JasonYangNWI commented 1 year ago

After barcode is scanned, software should release the camera resource. Otherwise, the camera can't be used by the rest of the app. Do you know how to release the resource? Thank you very much.

scriptBoris commented 1 year ago

This project is dead

aliegeni commented 1 year ago

It's also not possible to use NFC service in the rest of the app after scanning.

egorychev10 commented 1 year ago

I have the same problem on iOS

compvizard commented 1 year ago

You release the camera resource by disposing of the parent Shell class. Create a new Shell class and add a ShellContent whose ContentTemplate points to the ContentPage with the Zxing component. To access this scanner page, use these two lines of code:

Application.Current.MainPage = new YourScannerShellClass();
await Shell.Current.GoToAsync("yourscannerroute");

To navigate back to the main shell from the scanner page,

Application.Current.MainPage = new AppShell();
await Shell.Current.GoToAsync("yourlastmainshellroute");
viniciusverasdossantos commented 1 year ago

I have same problem. Is this project really dead? Is there another alternative?

JuhaKoivisto commented 1 year ago

I found this: https://www.youtube.com/watch?v=FuvFrIS9wm0

silmoonsone commented 1 year ago

I solved this. that not bug.

我将代码clone下来研究了一下,是因为DisconnectHandler(MAUI用于清理使用资源的方法)没有被触发,在MAUI中,Handler中的DisconnectHandler是需要手动触发的。 你需要在ContentPage的构造函数中:

Unloaded += (sender, e) => { cameraBarcodeReaderView.Handler.DisconnectHandler(); };

记住在XAML页面增加控件的标识名称:

<zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView" x:FieldModifier="public" />

其实说不是一个BUG的话,我感觉也是属于设计不合理,应该在控件订阅Unloaded方法自动调用DisconnectHandler,不需要使用者手动去调用的!

ref: https://learn.microsoft.com/zh-cn/dotnet/maui/user-interface/handlers/create?view=net-maui-7.0#native-view-cleanup

0x070696E65 commented 1 year ago

I had a same problem and solved by https://github.com/Redth/ZXing.Net.Maui/issues/118#issuecomment-1540628217. Thank you!

UkeHa commented 1 year ago

Here's a translation of @silmooninc's post:

I cloned the code and researched it because the DisconnectHandler (the method used by MAUI to clean up resources) was not triggered. In MAUI, the DisconnectHandler in the Handler needs to be triggered manually.
You need in the constructor of ContentPage:

         Unloaded += (sender, e) => { cameraBarcodeReaderView.Handler.DisconnectHandler(); };

Remember to add the identification name of the control to the XAML page:

        <zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView" x:FieldModifier="public" />

In fact, if it is not a BUG, I feel that it is also an unreasonable design. The DisconnectHandler should be automatically called in the control subscription Unloaded method, and the user does not need to call it manually!

If i do this, the camera will be released, but once in a while it throws "VirtualView cannot be null here".

silmoonsone commented 1 year ago

Here's a translation of @silmooninc's post:


I cloned the code and researched it because the DisconnectHandler (the method used by MAUI to clean up resources) was not triggered. In MAUI, the DisconnectHandler in the Handler needs to be triggered manually.

You need in the constructor of ContentPage:

         Unloaded += (sender, e) => { cameraBarcodeReaderView.Handler.DisconnectHandler(); };

Remember to add the identification name of the control to the XAML page:

        <zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView" x:FieldModifier="public" />

In fact, if it is not a BUG, I feel that it is also an unreasonable design. The DisconnectHandler should be automatically called in the control subscription Unloaded method, and the user does not need to call it manually!

If i do this, the camera will be released, but once in a while it throws "VirtualView cannot be null here".

根据我给的提示是没问题的,如果有问题找一下自己的其他原因,不过我现在已经弃用这个项目了,正在使用新的实现:https://youtu.be/FuvFrIS9wm0