Reputation: 629
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
Upvotes: 0
Views: 1013
Reputation: 8260
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.
Upvotes: 1