简体   繁体   中英

Generate and bind Barcode to image control in MAUI windows application

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM