0

I'm trying to use ZXing to render a simple barcode on screen in Xamarin. I've added the ZXing package to my solution and have the following code:

        BarcodeWriterPixelData writer = new BarcodeWriterPixelData
        {
            Format = BarcodeFormat.CODE_39 ,
            Options = new ZXing.Common.EncodingOptions{
                Height = 80,
                Width = 30,
                Margin = 0
            }
        };
        var data = writer.Write("18898798790");
        IDBarCode.Source = ImageSource.FromStream(() => new MemoryStream(data.Pixels));

However my code runs without rendering anything on screen. I've been looking for some sort of sample I can use without any sucess.

Kokul Jose
  • 1,384
  • 2
  • 14
  • 26
Amir Peivandi
  • 349
  • 4
  • 19

2 Answers2

1

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();
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
0

After trying tons of different things and getting nowhere and contacting the ZXing's dev team I gave up and used a different method.

I used a ZXing component instead if using a normal Image and all works fine now. Here is the code:

    private ZXingBarcodeImageView barcode=null;
    public MainPage()
    {
        InitializeComponent();
    }

    void Handle_Clicked(object sender, System.EventArgs e)
    {
        if (barcode==null){
            barcode = new ZXingBarcodeImageView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
            };
            barcode.BarcodeFormat = ZXing.BarcodeFormat.CODE_128;
            barcode.BarcodeOptions.Width = 500;
            barcode.BarcodeOptions.Height = 100;
            barcode.BarcodeValue = contentEntry.Text.Trim();
            barResult.Content = barcode;
        }else{
            barcode.BarcodeValue = contentEntry.Text.Trim();
        }

    }

barResult is just a simple ContentView with nothing in it.

        <ContentView x:Name="barResult">                
        </ContentView>
Amir Peivandi
  • 349
  • 4
  • 19