afriscic / BarcodeScanning.Native.Maui

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

Issue with Disabling Camera #63

Closed davidraona closed 5 months ago

davidraona commented 5 months ago

Title: Issue with Disabling Camera

Description:

I am experiencing an issue where the camera fails to disable when the Button_Released event is triggered in my application. The camera is initially set to be disabled, and it successfully enables on a button press but does not disable again on button release. The event is being triggered successfully but the camera remains active.

Environment: I just tried on Android 13

Here is a simplified version of the relevant code:

XAML:

<Button Text="Camera" VerticalOptions="Center" HorizontalOptions="Center" Pressed="Button_Pressed" Released="Button_Released"/>
<scanner:CameraView x:Name="Camera" CameraEnabled="False" CaptureQuality="Highest" HeightRequest="185"></scanner:CameraView>

C# Code-Behind:

private void Button_Pressed(object sender, EventArgs e)
{
    Camera.CameraEnabled = true;
}

private void Button_Released(object sender, EventArgs e)
{
    Camera.CameraEnabled = false;
}

Expected Behavior: The camera should be disabled when the button is released.

Actual Behavior: The camera remains enabled even after the button is released.

Steps to Reproduce:

Start the application. Press the Camera button to enable the camera. Release the Camera button. The camera should disable on step 3, but it remains active.

Please let me know if you need any further details or if there is any additional information I can provide to help resolve this issue.

JuanDLGZ commented 5 months ago

This issue also happened with MVVM,

davidraona commented 5 months ago

Actually, the problem occurs in scenarios where there is an attempt to change the CameraEnabled property from True to False. In my debugging, I noticed that the HandleCameraEnabled method in CameraManager is not being triggered after this change when using MVVM. When not using MVVM, it is triggered but has no effect.

To work around this, I used a DataTrigger in XAML that reinitializes the CameraView when CameraInitialized is True. This ensures that the camera is completely reset and not merely disabled, which helps to address the issue where the camera remains active despite the CameraEnabled property being set to False. This method might be particularly necessary on specific platforms like Android, where this behavior has been noted. Here's the modified XAML code for the workaround:

<ContentView>
    <ContentView.Triggers>
        <DataTrigger TargetType="ContentView" Binding="{Binding CameraInitialized}" Value="True">
            <Setter Property="Content">
                <Setter.Value>
                    <scanner:CameraView x:Name="Camera" CameraEnabled="{Binding IsCameraEnabled}" CaptureQuality="Highest" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </ContentView.Triggers>
</ContentView>

This solution ensures that the CameraView component is correctly updated, effectively managing the camera's enabled state as intended.

Olaf-R commented 5 months ago

FWIW, I have two ways how scanning can be performed - either manual or automatic. While the manual mode is active, users must keep the Scan-button pressed. I implemented this using the PauseScanning property which reacts to the Pressed- and Released-Commands of a button control.

I'm using the CameraEnabled property only within the page's OnAppearing and OnDisappearing events (otherwise all MVVM).

No problems here. I do think that this is the way it's supposed to work.

davidraona commented 5 months ago

Thank for answering, Olaf-R. I understand the purpose of PauseScanning property. However, my concern is specifically with controlling the camera's power state directly to manage battery consumption. We considered enabling and disabling the camera—not just pausing scanning—to save battery when the camera is not in use. Furthermore, we are using Views, so when the View changes the camera still active, what is a big problem for us.

Is there no built-in capability to toggle the CameraEnabled property on and off dynamically outside of the page lifecycle events? It seems somewhat limiting to have the CameraEnabled property only effective during the lifecycle of the component.

Thanks again for your input.

afriscic commented 5 months ago

Hello all and thank you for your feedback.

I managed to test one scenario this morning (ContentPage, code behind, basic Button_Pressed and Button_released events, Oneplus 7 PRO, Android 12) and both setting Enabled to true and false started and stopped the camera successfully. One thing to remember is that CameraView will retain the last frame when camera is disabled, but it will be frozen.

So far I haven't tried MVVM and ContentVIew.

Can you create a some basic MAUI project that expresses this bug and upload it here so I can download it and try to replicate it.

davidraona commented 5 months ago

You are right. Sorry for the inconvinience. The problem is actually only appearing when using MVVM. Hope this sample code helps to reproduce the problem. BarcodeScanningTest.zip

afriscic commented 5 months ago

Should be fixed in 1.5.2

davidraona commented 5 months ago

Perfect, the problem is solved. Thanks for your work.