Redth / ZXing.Net.Maui

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

Scanner doesn't work if the app doesn't use Shell ? #109

Open ThomasCarltonInPerson opened 1 year ago

ThomasCarltonInPerson commented 1 year ago

Hello community,

I tried using ZXing.Net.Maui scanner in an empty app.

I seems like it doesn't work if the app doesn't use shell.

Here is my app class code :

The scanner works if the app is initialized using this code :

` namespace MauiApp1;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new AppShell();
    }
}

`

However if the app is initialized using this code, it doesn't work. The camera view is just black and doesn't shod the camera feed.

` namespace MauiApp1;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
}

`

In both cases I'm creating the scanner by following the different steps in the documentation :

` <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI" x:Class="MauiApp1.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

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

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

`

` using ZXing.Net.Maui; namespace MauiApp1;

public partial class MainPage : ContentPage
{
    int count = 0;

    public MainPage()
    {
        InitializeComponent();
        cameraBarcodeReaderView.Options = new BarcodeReaderOptions
        {
            Formats = BarcodeFormats.OneDimensional,
            AutoRotate = true,
            Multiple = true
        };
    }
}

`

Does anyone know what am I doing wrong please ? My app doesn't use Shell. So I'm unable to use the scanner.

Thanks. Cheers,

thomaskaelin commented 1 year ago

@ThomasCarltonInPerson I just wrote a little prototype without Shell, using ZXing.Net.Maui.Controls in version 0.3.0-preview.1. It was working fine for me. The only difference to your solution: I was placing CameraBarcodeReaderView not directly on MainPage.

In App.xaml.cs:

MainPage = new NavigationPage(new MainPage());

In MainPage.xaml.cs:

var scanPage = new ScanPage();
await Navigation.PushAsync(scanPage);

In ScanPage.xaml.cs:

const BarcodeFormat formatsToRecognize = BarcodeFormat.Code39;

CameraBarcodeReaderView.Options = new BarcodeReaderOptions
{
    AutoRotate = true,
    Formats = formatsToRecognize,
    Multiple = false,
    TryHarder = true,
    TryInverted = false
};
CameraBarcodeReaderView.CameraLocation = CameraLocation.Rear;
CameraBarcodeReaderView.IsTorchOn = false;
CameraBarcodeReaderView.IsDetecting = true;
CameraBarcodeReaderView.BarcodesDetected += (_, args) =>
{
    // Handle results
};

Maybe your code is not working because you are not setting IsDetecting = true?