0

I am working on the MAUI windows application. I want to bind the Image Source from the generated barcode. I am using the following package for the generating barcode.

https://www.nuget.org/packages/barcode#:~:text=The%20.,Plessey%2C%20USPS%2C%20and%20QR.

I don't want to save the barcode image file. I want to just generate a barcode for the string property, and convert it to the appropriate format which can be bound to the ImageSource property. I am not getting any exceptions but the Barcode image is not displayed in my UI. The piece of code which I have tried.

public void GenerateQR()
        {
            //ToDo: Generate QR from last Enqueue data. Use QR generation library.
            var myBarcode = BarcodeWriter.CreateBarcode(QrStringValue, BarcodeWriterEncoding.QRCode);
            if (myBarcode.Verify())
            {
                QrCode = ImageSource.FromStream(() => myBarcode.ToStream());
            }
        }

Here, QrStringValue is a string property that keeps changing on the button Click event.

private string _qrStringValue;
        public string QrStringValue
        {
            get => _qrStringValue;
            set
            {
                _qrStringValue = value;
                OnPropertyChanged(nameof(QrStringValue));
            }
        }

And QrCode is the ImageSource property that binds to the Image control.

private ImageSource _qrCode;
        public ImageSource QrCode
        {
            get => _qrCode;
            set
            {
                _qrCode = value;
                OnPropertyChanged(nameof(QrCode));
            }
        }

<Image Source="{Binding SharedVM.QrCode}" Margin="90"/>

Do I need to go with some MAUI-specific library for barcode generation and display like the following? https://github.com/Redth/ZXing.Net.Maui

RMR
  • 599
  • 3
  • 12
  • 35
  • first, I would verify that `myBarcode.ToStream()` is returning valid image data. A quick look at their docs show a number of different properties and methods for returning different image formats. Their default is bmp, which XF does not support – Jason Nov 16 '22 at 17:59
  • How I can verify whether it is returning valid data or not? – RMR Nov 16 '22 at 18:07
  • 1
    Write it to a file and examine it with an image tool – Jason Nov 16 '22 at 18:12
  • ToStream() returns a stream with a length of 2820. – RMR Nov 16 '22 at 18:38
  • 1
    that doesn't tell you anything about whether or not its a valid image file – Jason Nov 16 '22 at 20:20

1 Answers1

1

You could try the following code which worked for me:

public void GenerateQR()
{
    //ToDo: Generate QR from last Enqueue data. Use QR generation library.
    var myBarcode = BarcodeWriter.CreateBarcode(QrStringValue, BarcodeWriterEncoding.QRCode);
    if (myBarcode.Verify())
    {
        QrCode = ImageSource.FromStream(() => new MemoryStream(myBarcode.ToPngBinaryData()));
    }   
}    

That seems BarcodeWriter.CreateBarcode not return the standard format.

Hope it works for you.

Liqun Shen-MSFT
  • 3,490
  • 2
  • 3
  • 11