Barney Chambers
Barney Chambers

Reputation: 2783

Xamarin - Display QR code in form

I am writing an application in Xamarin to create a QR code for a given input.

using ZXing.Net.Mobile.Forms;

var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new EncodingOptions
            {
                Height = 200,
                Width = 600
            }
        };
        var bitmap = writer.Write("Hello Stack Overflow");

How do I now display this barcode on my form?

Upvotes: 2

Views: 1801

Answers (1)

Jesus Angulo
Jesus Angulo

Reputation: 2666

You should use a ZXingBarcodeImageView

using System;
using Xamarin.Forms;
using System.Threading.Tasks;
using ZXing.Net.Mobile.Forms;

public class BarcodePage : ContentPage
{
    ZXingBarcodeImageView barcode;

    public BarcodePage ()
    {
        barcode = new ZXingBarcodeImageView {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            AutomationId = "zxingBarcodeImageView",
        };
        barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
        barcode.BarcodeOptions.Width = 300;
        barcode.BarcodeOptions.Height = 300;
        barcode.BarcodeOptions.Margin = 10;
        barcode.BarcodeValue = "Hello Stack Overflow";

        Content = barcode;
    }
}

You can check the full sample in Github https://github.com/Redth/ZXing.Net.Mobile/blob/master/Samples/Forms/Core/BarcodePage.cs

Upvotes: 3

Related Questions