BarcodeScanner.Mobile icon indicating copy to clipboard operation
BarcodeScanner.Mobile copied to clipboard

Camera black screen when switched away and back using AppShell tab

Open programatix opened this issue 2 years ago • 9 comments

Describe the bug I have the camera view in a view as a tab in AppShell. On navigated to the tab with the camera, it works. Upon navigated away and back to the tab, the camera view is black.

The camera can be brought back to life by toggling the front/rear camera. So, I suspect the issue is with the camera not being re-initialized upon returning back to the page? Would be great if can manually turn on/off the camera.

Sample AppShell

<Shell ...>
    <TabBar>
        <Tab ... />
        </Tab>
        <Tab Title="Scan">
            <ShellContent ContentTemplate="{DataTemplate pages:ScanPage}" Route="Scanner" />
        </Tab>
        <Tab ... />
        </Tab>
    </TabBar>
</Shell>

I get the same result for net6.0-android and net7.0-android.

To Reproduce Steps to reproduce the behavior:

  1. Go to tab with camera view -> OK
  2. Navigate away from the tab to another tab.
  3. Navigate back to the tab with camera view.
  4. Camera view is black

Expected behavior Camera view display the camera.

Smartphone (please complete the following information):

  • Device: Android Emulator, Z Fold 4.
  • OS: Android 13
  • Version Maui 7.0.52/7.0.100 VS 17.4.33205.214

Workaround Handle the OnAppearing event to toggle the camera to rear/front. It will "wake" the camera up.

programatix avatar Dec 16 '22 07:12 programatix

Yup. I have the same behavior.

Alexbits avatar Feb 21 '23 17:02 Alexbits

Я использую вот такой алгоритм в MAUI, достаточно при переключении навигации, переключаться между камерами Front и Back. Так же сюда поместил запрос доступа к камере, если пользователь его ещё не давал.
// Получение вкладки
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);

        // Запрашиваем и проверяем доступ к камере
        bool allowed = await BarcodeScanner.Mobile.Methods.AskForRequiredPermission();

        // Перезапускаем поток камеры
        Scaner.CameraFacing = CameraFacing.Front;
        Scaner.CameraFacing = CameraFacing.Back;
    }

Trainian avatar Apr 03 '23 23:04 Trainian

Same problem fom me. Workaround works fine. Is it possible to have a correction ? Thanks

be-swarm avatar May 22 '23 13:05 be-swarm

Same problem and also for me the workaround works fine. Thanks a lot!

MarcoDP65 avatar Jun 13 '23 11:06 MarcoDP65

Я использую вот такой алгоритм в MAUI, достаточно при переключении навигации, переключаться между камерами Front и Back. Так же сюда поместил запрос доступа к камере, если пользователь его ещё не давал.
// Получение вкладки
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);

        // Запрашиваем и проверяем доступ к камере
        bool allowed = await BarcodeScanner.Mobile.Methods.AskForRequiredPermission();

        // Перезапускаем поток камеры
        Scaner.CameraFacing = CameraFacing.Front;
        Scaner.CameraFacing = CameraFacing.Back;
    }

This option worked for me aswell, thank you.

bruce-rapiddev avatar Oct 27 '23 08:10 bruce-rapiddev

Я использую вот такой алгоритм в MAUI, достаточно при переключении навигации, переключаться между камерами Front и Back. Так же сюда поместил запрос доступа к камере, если пользователь его ещё не давал.
// Получение вкладки
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);

        // Запрашиваем и проверяем доступ к камере
        bool allowed = await BarcodeScanner.Mobile.Methods.AskForRequiredPermission();

        // Перезапускаем поток камеры
        Scaner.CameraFacing = CameraFacing.Front;
        Scaner.CameraFacing = CameraFacing.Back;
    }

This option worked for me aswell, thank you.

Unfortunately after updating to net8.0 this trick no longer works.

owik100 avatar Nov 15 '23 10:11 owik100

I found a workaround :

1- Create a QRCode xaml view like this :

<gv:CameraView xmlns:gv="clr-namespace:BarcodeScanner.Mobile;assembly=BarcodeScanner.Mobile.Maui" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BooksApp.Pages.QRCode" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" TorchOn="False" VibrationOnDetected="True" ScanInterval="50" />

for xaml.cs :

using BarcodeScanner.Mobile; namespace BooksApp.Pages;

public partial class QRCode : CameraView { public QRCode() { InitializeComponent(); } }

2- Call a Load function from the constructor in your page :

public QrScanner()

{ InitializeComponent();

    Load();

}

QRCode view = null; public async void Load() { if (view != null) { // Remove all the children of the page if there is already a qrcode view on the page stack.Children.Clear(); }

// Create a new instance of QRCode to add to your page view = new(); stack.Add(view); }

3- Call the Load function each time you change of page :

protected override async void OnNavigatedTo(NavigatedToEventArgs args) { base.OnNavigatedTo(args);

    Load();

}

Gribdall avatar Nov 20 '23 15:11 Gribdall

Thanks. I also noticed if i navigate to another view using await Shell.Current.GoToAsync and then go back to view with scanner this trick is no need and also throw exception. So i have to check from with view i return and ignore trick if needed.

owik100 avatar Nov 29 '23 21:11 owik100

Looks like in CameraView he is doing Handler.DisconnectHandler() when the view is Unloaded, which causes issues when navigating back to it

Jake-Derrick avatar Jan 10 '24 05:01 Jake-Derrick