I’m trying to get a QR code generated on the server and put it in an image in the application.NET MAUI.
My HTTPGET method on server ASP.NET:
[HttpGet("{sum}")]
public async Task <ActionResult<byte[]>> Get(string sum)
{
QrCodeEncodingOptions options = new()
{
DisableECI = true,
CharacterSet = "utf-8",
Width = 400,
Height = 400
};
var writer = new ZXing.Windows.Compatibility.BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
var QRcode = writer.Write(paymenMessage);
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(QRcode, typeof(byte[]));
}
else
return BadRequest();
}
}
I have omitted some lines of code that are not important for the question.
It is important to note that ImageConverter
converts the Bitmap QRCode
into an array with quotes ("
), which have to be removed manually.
My method of getting an array of bytes in a mobile application:
private async void Purchase(object obj)
{
if (sum >0 && productsInBasket is not null)
{
byte[] QRCode = await httpClient.GetByteArrayAsync($"http://10.0.2.2:5125/api/Qr/{sum}");
string sdw = Encoding.UTF8.GetString(QRCode);
byte[] qrrWithoutQuotes = Encoding.UTF8.GetBytes(sdw.Replace(""", String.Empty));
Dictionary<string, object> data = new Dictionary<string, object>
{
{"QRCode", qrrWithoutQuotes }
};
await Shell.Current.GoToAsync("//QR", data);
}
}
After that, I convert the byte array in the image using the function provided by CommunityToolkit.Maui:
<Frame HasShadow="False" WidthRequest="400" HeightRequest="400" HorizontalOptions="Center" VerticalOptions="Center">
<Image HeightRequest="400" WidthRequest="400" Source="{Binding QRCode, Mode=OneWay, Converter={StaticResource ByteToImage}}"/>
</Frame>
ViewModel:
[QueryProperty(nameof(QRCode), "QRCode")]
public class qrViewModel : INotifyPropertyChanged
{
#region Fields
private byte[] qrCode;
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Constructor
public qrViewModel()
{
}
public byte[] QRCode
{
get { return qrCode; }
set
{
qrCode = value;
NotifyPropertyChanged();
}
}
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
As you understood, that image was not displayed. I started to study this problem and passed a variable containing the value of a byte array to Entry and compared it with what the server provides me when requesting through the browser bar.
It turned out that when receiving data from the server, the + signs were completely deleted. This deletion happens regardless of whether I remove the quotes or not. Perhaps some other signs are being deleted.