BarcodeScanner.Mobile
BarcodeScanner.Mobile copied to clipboard
Camera black screen when switched away and back using AppShell tab
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:
- Go to tab with camera view -> OK
- Navigate away from the tab to another tab.
- Navigate back to the tab with camera view.
- 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.
Yup. I have the same behavior.
Я использую вот такой алгоритм в 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;
}
Same problem fom me. Workaround works fine. Is it possible to have a correction ? Thanks
Same problem and also for me the workaround works fine. Thanks a lot!
Я использую вот такой алгоритм в 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.
Я использую вот такой алгоритм в 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.
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();
}
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.
Looks like in CameraView he is doing Handler.DisconnectHandler() when the view is Unloaded, which causes issues when navigating back to it