вівторок, 26 травня 2020 р.

QRCode\BarCode in your C#\Xamarin application

Below is really working C#\Xamarin code, tested. You'll be need to install ZXing.Mobile packet. I realised it by adding DependencyService to my project.

OK. Thanks to the posts above, I managed to get this working so I thought I'd post code for anyone having issues in the future. Remember, this is solely around the display of a barcode NOT the scanning!

I added ZXing.Net.Mobile to my Droid and iOS projects. I didn't need to add it to my core Forms project.

In my core Forms project I had an image in XAML:

<Image Grid.Row="13" Grid.ColumnSpan="2" Source="{Binding BarcodeImageSource}" HorizontalOptions="Center" WidthRequest="300" HeightRequest="130"/>

On my view model I had:

public ImageSource BarcodeImageSource { get;set;}
....
// this will set the image source for the BarcodeImageSource property which the Image in XAML will pick up 
var stream = this.barcodeService.ConvertImageStream(result.CardId);
this.BarcodeImageSource =  ImageSource.FromStream(() => {return stream; });

Ok that bit of code 'this.barcodeService.ConvertImageStream(result.CardId);' had to use dependency injection to have Droid and iOS specific code. The ZXing code is the same in both cases. It's actually the code for writing the bitmap to a stream that is platform specific.

So I have my interface:

using System;
using System.IO;

namespace MyApp.Services
{
  public interface IBarcodeService 
  {   
      Stream ConvertImageStream(string text, int width = 300, int height=130);    
  }
}

Then Android implementation:

using Android.Graphics;
using MyApp.Services
using System;
using System.IO;
using ZXing.Mobile;

namespace MyApp.Android
{
  public class BarcodeService : IBarcodeService
  {
      public Stream ConvertImageStream(string text,int width = 300, int height=130)
      {
          var barcodeWriter = new ZXing.Mobile.BarcodeWriter {
                      Format = ZXing.BarcodeFormat.CODE_39,
                      Options = new ZXing.Common.EncodingOptions {
                          Width = width,
                          Height = height,
                                              Margin = 10
                      }
                  };

          barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
          var bitmap = barcodeWriter.Write(text);
          var stream = new MemoryStream();
          bitmap.Compress(Bitmap.CompressFormat.Png,100,stream);  // this is the diff between iOS and Android
          stream.Position = 0;
          return stream;
      }
  }
}

And the iOS implementation.

using System;
using System.Drawing;
using System.IO;
using Xamarin.Forms;
using MyApp.Services;
using UIKit;
using CoreGraphics;
using ZXing.Mobile;

namespace MyApp.iOS
{
  public class BarcodeService : IBarcodeService
  {
      public Stream ConvertImageStream(string text,int width = 300, int height=130)
      {
          var barcodeWriter = new ZXing.Mobile.BarcodeWriter {
                      Format = ZXing.BarcodeFormat.CODE_39,
                      Options = new ZXing.Common.EncodingOptions {
                          Width = width,
                          Height = height,
                                              Margin = 10
                      }
                  };
          barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
          var bitmap = barcodeWriter.Write(text);
          var stream = bitmap.AsPNG().AsStream(); // this is the difference 
          stream.Position = 0;

          return stream;
      }
  }
}

Немає коментарів:

Дописати коментар