Ashish Kumar
Ashish Kumar

Reputation: 765

Generate Bar Code in Xamarin Forms

I need to generate a Bar Code in my Xamarin.Forms project. I have found samples to scan Bar Code. Anyone has tried to generate the Bar Code ?

Upvotes: 0

Views: 998

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34118

With the ZXing library, you can not only scan barcodes but also generate them.

The sample repo can be found here: https://github.com/jfversluis/Blurry-ZXingBarcodeImageView and was created to answer another question here on StackOverflow about barcodes being blurry. But it also shows how to generate a barcode.

Basically, you just install the ZXing library onto your projects and add a ZXingBarcodeImageView to your page.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:BlurryZXing" x:Class="BlurryZXing.MainPage" xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" xmlns:zx="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable">
    <forms:ZXingBarcodeImageView 
        IsVisible="True"
        x:Name="QRCodeView"
        BarcodeFormat="QR_CODE" 
        HeightRequest="300"
        WidthRequest="300"
        BarcodeValue="-1">
        <zx:ZXingBarcodeImageView.BarcodeOptions>
            <zxcm:EncodingOptions Width="300" Height="300" />
        </zx:ZXingBarcodeImageView.BarcodeOptions>
    </forms:ZXingBarcodeImageView>
</ContentPage>

You can specify the format and value. Supported formats at time of writing are: UPC-A, UPC-E, EAN-8, Code 39, EAN-13, ITF, RSS-14, RSS-Expanded, QR Code, Code 93, Data Matrix, Code 128, Aztec (beta), Codabar, PDF 417 (beta), MaxiCode.

Don't forget to initialize the ZXing library in your platform projects, else it will show up empty. You can do this by adding:

// On iOS in your AppDelegate.cs in the FinishedLaunching method
ZXing.Net.Mobile.Forms.iOS.Platform.Init();

and

// On Android in the MainActivity.cs in the OnCreate method
ZXing.Net.Mobile.Forms.Android.Platform.Init();

Upvotes: 3

Related Questions