Redth / ZXing.Net.Maui

Barcode Scanning for MAUI?
MIT License
434 stars 143 forks source link

ZXing.Net.Maui doesn't seem to be able scan more complicated PDF417 barcodes #174

Open gtvracer opened 3 months ago

gtvracer commented 3 months ago

Using ZXing.Net.Maui.Controls v0.4.0 and the basic ZXing page, and configuration, it can detect simple PDF417, but it fails to detect more complex ones, like the one on the back of a California drivers license. It also doesn't keep the torch on either, but flashes once when the page initializes. I've tried the barcode images samples on the internet by searching for "PDF417 images", only the simple ones are read....

The Xamarin version of ZXing is able to read the CA drivers license barcode, fyi..

I'm using Visual Studio v17.9.2, XCode 15.3 and .net8.

Any insight will be appreciated. Thanks!

Page:

<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="MyProject.Views.ScanPage"
             Title="ScanPage">   
        <zxing:CameraBarcodeReaderView
            x:Name="barcodeView"
                        IsTorchOn="True"
            BarcodesDetected="BarcodesDetected" />        
</ContentPage>

Code behind:

public partial class ScanPage : ContentPage
{
    public ScanPage()
    {
        InitializeComponent();
        BindingContext = this;

        barcodeView.Options = new ZXing.Net.Maui.BarcodeReaderOptions()
        {
            TryHarder = true,
            AutoRotate = true,
            Multiple = true,
            Formats = BarcodeFormats.TwoDimensional // | ZXing.Net.Maui.BarcodeFormat.QrCode | BarcodeFormat.Pdf417 
        };
        barcodeView.IsTorchOn = true;
    }

    protected void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
    {
        foreach (var barcode in e.Results)
        { // dumps to Debug.WriteLine()...
            App.Log($"Barcode: {barcode.Format} -> {barcode.Value}");
        }

        var first = e.Results?.FirstOrDefault();
        if (first != null)
        {
            Dispatcher.DispatchAsync(async () =>
            {
                await DisplayAlert("Barcode detected", first.Value, "OK");
            });
        }
    }
}

MauiProgram.cs:

  public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .UseMauiCommunityToolkit()
                .UseBarcodeReader()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                })
                .ConfigureMauiHandlers(handlers =>
                {
                    handlers.AddHandler(typeof(PillCheckbox), typeof(PillCheckboxRenderer));
                });

            builder.Services.AddSingleton<IImageInfo, ImageInfo>();

#if DEBUG
            builder.Logging.AddDebug();
#endif

            var app = builder.Build();

            ServiceProviderHelper.Initialize(app.Services);
            return app;
        }
    }
chuckgiddens commented 3 months ago

Add

.ConfigureMauiHandlers(h => { h.AddHandler(typeof(CameraBarcodeReaderView),typeof(CameraBarcodeReaderViewHandler)); h.AddHandler(typeof(CameraView), typeof(CameraViewHandler)); h.AddHandler(typeof(BarcodeGeneratorView), typeof(BarcodeGeneratorViewHandler)); });

To MauiProgram.cs

gtvracer commented 3 months ago

Hi Chuck,

I did as you suggested, captured an image using MediaPicker.CapturePhotoAsync() and fed into the parsing code as below. It returns a null result for that image attached.

// code BarcodeReader reader = new BarcodeReader { AutoRotate = true,

Options = new DecodingOptions {
    TryHarder = true,
    PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 }
}

}; // load a bitmap try { SKBitmap originalBitmap = SKBitmap.Decode(photoPath); // detect and decode the barcode inside the bitmap var result = reader.Decode(originalBitmap); // do something with the result if (result != null) { App.Log("Succeeded in parsing barcode"); App.Log(result.BarcodeFormat.ToString()); App.Log(result.Text); } else App.Log("Failed to parse barcode"); } catch (Exception ex) { App.Log($"ParseScannedImage exception:{ex.Message}"); } // end code

[image: pdf417-1.PNG]

On Mon, Apr 8, 2024 at 7:57 AM Chuck Giddens @.***> wrote:

Add

.ConfigureMauiHandlers(h => {

h.AddHandler(typeof(CameraBarcodeReaderView),typeof(CameraBarcodeReaderViewHandler)); h.AddHandler(typeof(CameraView), typeof(CameraViewHandler)); h.AddHandler(typeof(BarcodeGeneratorView), typeof(BarcodeGeneratorViewHandler)); });

To MauiProgram.cs

— Reply to this email directly, view it on GitHub https://github.com/Redth/ZXing.Net.Maui/issues/174#issuecomment-2042983435, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACDPXLROXK7UVGGJQOJP333Y4KV5FAVCNFSM6AAAAABEYHQZWSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBSHE4DGNBTGU . You are receiving this because you authored the thread.Message ID: @.***>

gtvracer commented 3 months ago

Here is the link to image: https://www.bing.com/images/search?view=detailV2&ccid=Ss6OGv4g&id=E6A2E8947ADD25E7314DD826338FFE6EF9CA9A65&thid=OIP.Ss6OGv4g3YJACytkgc4l-AHaEX&mediaurl=https%3a%2f%2fidtempl.com%2fwp-content%2fuploads%2f2022%2f04%2fScreenshot-2022-06-04-135541.jpg&cdnurl=https%3a%2f%2fth.bing.com%2fth%2fid%2fR.4ace8e1afe20dd82400b2b6481ce25f8%3frik%3dZZrK%252bW7%252bjzMm2A%26pid%3dImgRaw%26r%3d0&exph=730&expw=1236&q=driver+license+pdf417+images&simid=608009873378706014&FORM=IRPRST&ck=265CE94CD1C8581457D5EBA75B6B4D6F&selectedIndex=0&itb=0&ajaxhist=0&ajaxserp=0

codebeaulieu commented 1 month ago

I'm encountering the same issue. The camera won't pick up anything. When I attempt to feed a high res image into the decoder directly, I get null back.