Search code examples
c#mauizxing

MAUI / .NET 8 - QR Code Scanner (ZXing) not detecting codes


I am implementing a QR code scanner in a MAUI app, running on .NET 8.

The app is currently for Android, and will then be ported to IOS.

As a result, I settled on using ZXing.Net.Maui and following the documentation here: https://github.com/Redth/ZXing.Net.Maui#barcode-scanning

My problem is that QR codes remain undetected and BarcodesDetected() is not being accessed if tested with a breakpoint against it.

Am I missing something?

I did find this post (ZXing for MAUI (MobileBarcodeScanner)), which seems to be implementing the same process as me, but no success on my end.

MauiProgram.cs

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();

    builder
            .UseMauiApp<App>()
            )
            .UseMauiCommunityToolkit()
            .UseFFImageLoading()
            .UseBarcodeReader()

    var app = builder.Build();

    return app;
}

AndroidManifest.xaml:

<uses-permission android:name="android.permission.CAMERA"/>

View.xaml:

<?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.Controls"
             x:Class="Project.Views.View">
    <Grid>
        <zxing:CameraBarcodeReaderView
                x:Name="cameraBarcodeReaderView"
                BarcodesDetected="BarcodesDetected"
                IsDetecting="True"
                CameraLocation="Rear"
                IsEnabled="True"/>
    </Grid>
</ContentPage>

view.xaml.cs:

public partial class View : ContentPage
{
    public View()
    {
        InitializeComponent();
        CheckCameraPermission();

        cameraBarcodeReaderView.Options = new BarcodeReaderOptions
        {
            Formats = BarcodeFormats.OneDimensional,
            AutoRotate = true,
            Multiple = true
        };
    }

    private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
    {
        foreach (var barcode in e.Results)
            Console.WriteLine($"Barcodes: {barcode.Format} -> {barcode.Value}");
    }
}

Solution

  • for QR scanning, this

    Formats = BarcodeFormats.OneDimensional,
    

    should be

    Formats = BarcodeFormats.TwoDimensional,