I am trying to generate and display barcode in Xamarin forms using following code.
barcode = new ZXingBarcodeImageView
{
HeightRequest = 400,
WidthRequest = 400
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
barcode.BarcodeOptions.Width = 400;
barcode.BarcodeOptions.Height = 400;
barcode.BarcodeOptions.Margin = 10;
barcode.BarcodeValue = "ZXing.Net.Mobile";
Content = barcode;
The QR code is rendered fine in Android virtual device. On the other hand, in iOS device, a distorted QR code is rendered. The aspect ration of QR code rendered on iOS virtual device is not square as it supposed to be.
The QR code rendered on Android virtual device is verified using a mobile phone.
Edit: Tried the following code as suggested by @wilson
try
{
barcode = new ZXingBarcodeImageView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
barcode.BarcodeOptions.Width = 500;
barcode.BarcodeOptions.Height = 500;
barcode.BarcodeOptions.Margin = 10;
barcode.BarcodeValue = "ZXing.Net.Mobile";
Content = barcode;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
Still, I see the same problem. When I tried the example code of @wilson and it works fine on iPhone simulator. Probably I should use content view as suggested.
Edit 2 I tried the stack layout as shown below.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AZCommute"
x:Class="AZCommute.MainPage">
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="QR Code Generator" HorizontalOptions="Center"/>
<local:QRResult x:Name="qrResult"/>
</StackLayout>
</ContentPage>
But, it was of no help. Nothing rendered on iOS. I tried by adding WidthRequest
and HeightRequest
while initializing the ZXingBarcodeImageView
as shown below.
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill,
WidthRequest = 410,
HeightRequest = 410,
Now, ZXingBarcodeImageView
renders in iPhone simulator. But, the aspect ratio is lost (height > width ).
Note: I am using the ContentView
with StackLayout