Generate Imagesource with ZXing.Net.Mobile in UWP

0

Problem

I'm trying using ZXing.Net.Mobile to create barcode image in UWP project. I found this approach.

But in line var wb = result.ToBitmap() as WriteableBitmap; I'm getting result as byte[], which doesn't have method ToBitmap().

Then I found this straight forward code

var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var wb = writer.Write("12345678");
BarcodeImg.Source = wb;

but last lane throws error byte[] cannot be converted to ImageSource

In my naive mind I thought "Ok that should be easy". HA! Everywhere I look I find something similar to this answer.

BitmapImage doesn't have methods BeginInit and EndInit.

Question

How I can convert byte[] to ImageSource or use ZXing to create one?

I'm stuck, again I'm not familiar with UPW. I'll be thankful for each tip.

Update

Ok at this moment situtation looks like this

private async void updateBarcodeImg(string code) {
    var writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.QR_CODE;
    var wb = writer.Write(code) as Byte[];
    try {
        BarcodeImg.Source = await ImageFromBytes(wb);
    } catch (Exception e) {
        Debug.WriteLine(e.Message);
    }
}

public async static Task<BitmapImage> ImageFromBytes(Byte[] bytes) {
    BitmapImage image = new BitmapImage();
    using (IRandomAccessStream stream = bytes.AsBuffer().AsStream().AsRandomAccessStream()) {
        stream.Seek(0);
        await image.SetSourceAsync(stream);
    }
    return image;
}

Update

And in line await image.SetSourceAsync(stream); an exception is throwing "Exception from HRESULT: 0x88982F50". Google says that this is because stream is not set to position 0. But I do it a one line earlier.

c#
uwp
zxing
asked on Stack Overflow Oct 6, 2016 by Błażej • edited May 23, 2017 by Community

1 Answer

3

According to your question, you are going to use ZXing.Net.Mobile to create barcode image. However the sample you've found is using ZXing.Net. They are not the same library.

To use ZXing.Net.Mobile, we need install ZXing.Net.Mobile from NuGet:

Install-Package ZXing.Net.Mobile

And then use following code:

var writer = new ZXing.Mobile.BarcodeWriter
{
    Format = ZXing.BarcodeFormat.QR_CODE,
    Options = new ZXing.Common.EncodingOptions
    {
        Height = 300,
        Width = 300
    },
    Renderer = new ZXing.Mobile.WriteableBitmapRenderer() { Foreground = Windows.UI.Colors.Black }
};
var writeableBitmap = writer.Write("https://developer.microsoft.com/en-us/windows/windows-apps");

QrCodeImg.Source = writeableBitmap;

Also we can use ZXing.Net by install ZXing.Net from NuGet:

Install-Package ZXing.Net

And use the code in the sample:

ZXing.IBarcodeWriter writer = new ZXing.BarcodeWriter
{
    Format = ZXing.BarcodeFormat.QR_CODE,//Mentioning type of bar code generation
    Options = new ZXing.Common.EncodingOptions
    {
        Height = 300,
        Width = 300
    },
    Renderer = new ZXing.Rendering.PixelDataRenderer() { Foreground = Windows.UI.Colors.Black }//Adding color QR code
};
var result = writer.Write("http://www.bsubramanyamraju.blogspot.com ");
var wb = result.ToBitmap() as Windows.UI.Xaml.Media.Imaging.WriteableBitmap;
//Displaying QRCode Image
QrCodeImg.Source = wb;

Please note that we can not use these two libraries at the same time. Please make sure you are using the right code that matches the library you've chosen.

answered on Stack Overflow Oct 7, 2016 by Jay Zuo

User contributions licensed under CC BY-SA 3.0