I am using ZXing to generate a QR code. This is what my code looks like:
public partial class QRPage : ContentPage
{
public QRPage()
{
InitializeComponent();
var stream = DependencyService.Get<IBarcodeService>().ConvertImageStream("nika");
qrImage.Source = ImageSource.FromStream(() => { return stream; });
qrImage.HeightRequest = 200;
}
}
And the other part:
[assembly: Xamarin.Forms.Dependency(typeof(BarcodeService))]
namespace MyApp.Droid
{
public class BarcodeService : IBarcodeService
{
public Stream ConvertImageStream(string text, int width = 500, int height = 500)
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = width,
Height = height,
Margin = 2
}
};
barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
var bitmap = barcodeWriter.Write(text);
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
stream.Position = 0;
return stream;
}
}
}
Here is the xaml where I'm using the code:
<StackLayout Padding="20,30,20,30">
<Label Text="..." FontSize="Medium "/>
<Frame VerticalOptions="CenterAndExpand">
<Image x:Name="qrImage" WidthRequest="300" />
</Frame>
...
</StackLayout>
The problem is, that no matter what do I set as height and width for ConvertImageStream
, the resulting image is not square, but rather looks like this:
How can I turn it into a square? Thanks in advance.