ape
ape

Reputation: 13

.NET MAUI barcode scanner does not work for 1D codes

I have a problem with scanning codes using the Camera.MAUI library. When I scan QR codes, everything works as it should. When I want to scan a 1D code, the scanner does not detect it. I tried various barcodes from the Internet and from products (e.g. UHT milk barcode). It doesn't detect this code every time.

I create applications in .net 7.0. Below is the code.

*.xaml file:

<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="InvScanPro.Views.ScannerPage">
    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30"
            VerticalOptions="Center">
            
            <cv:CameraView
                x:Name="CameraView"
                HeightRequest="450"
                WidthRequest="350"
                CamerasLoaded="CameraView_CamerasLoaded"
                BarCodeDetectionEnabled="True"
                BarcodeDetected="CameraView_BarcodeDetected"/>

        </VerticalStackLayout>
    </ScrollView>
</toolkit:Popup>

*.xaml.cs file:

public partial class ScannerPage : Popup
{
    private bool _isScanning;

    public ScannerPage()
    {
        InitializeComponent();

        InitializeBarCodeOptions();
    }

    private void InitializeBarCodeOptions()
    {
        CameraView.BarCodeOptions = new BarcodeDecodeOptions
        {
            AutoRotate = true,

            PossibleFormats =
            {
                ZXing.BarcodeFormat.QR_CODE,
                ZXing.BarcodeFormat.All_1D
            }
        };
    }

    private void CameraView_CamerasLoaded(object sender, EventArgs e)
    {
        if (CameraView.Cameras.Count <= 0) return;

        CameraView.Camera = CameraView.Cameras.First();
        MainThread.BeginInvokeOnMainThread(async () =>
        {
            await CameraView.StopCameraAsync();
            await CameraView.StartCameraAsync();
        });
    }

    private void CameraView_BarcodeDetected(object sender, Camera.MAUI.ZXingHelper.BarcodeEventArgs args)
    {
        if (_isScanning) return;

        MainThread.BeginInvokeOnMainThread(async () =>
        {
            _isScanning = true;
            Vibration.Default.Vibrate(new TimeSpan(0, 0, 0, 0, 100));
            await CameraView.StopCameraAsync();
            await CloseAsync(args.Result[0].Text);
        });
    }
}

MauiProgram.cs

        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()            
            .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
        })
            .UseMauiCameraView()
            .UseMauiCommunityToolkit();

Upvotes: 0

Views: 1169

Answers (1)

Roberto Patuelli
Roberto Patuelli

Reputation: 1

It's a known behavior, it works but you have to rotate the reader (the phone) in landscape/horizontal or the barcode in vertical, if someone has a solution to keep the vertical view thanks in advance...

Upvotes: 0

Related Questions