4

I have been searching the web for examples of code using Zxing in Windows Phone 8.1 but have come up short. I am writing in C# and below is my code, which I have come up with so far:

BarcodeWriter _writer = new BarcodeWriter();

var hello =  _writer.Encoder.encode("HelloWhoIsThere", BarcodeFormat.QR_CODE, 350, 350);

ZXing.Common.BitMatrix matrix = new ZXing.Common.BitMatrix(359,350);

ZXing.Rendering.PixelData rendered = _writer.Renderer.Render(hello, BarcodeFormat.CODE_128, "HelloWhoIsThere");

byte[] byte1 = rendered.Pixel;

Stream memStream = new MemoryStream(byte1);

memStream.Position = 0;

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());

// create a new stream and encoder for the new image
InMemoryRandomAccessStream mrAccessStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(mrAccessStream, decoder);

// convert the bitmap to a 400px by 400px bitmap
encoder.BitmapTransform.ScaledHeight = 350;
encoder.BitmapTransform.ScaledWidth = 350;

// write out to the stream
try
{
    await encoder.FlushAsync();
}
catch (Exception ex)
{
    string s = ex.ToString();
}

// render the stream to the screen
WB = new WriteableBitmap(350, 350);
WB.SetSource(mrAccessStream);
if (WB != null)
{
    SelectedImage.Source = WB;
}
if (WB == null)
{
    txtDecoderContent.Text = "WB = null";
}

I Get an error of "System.NullReferenceException: Object reference not set to an instance of an object." which I think happens when I try and convert the rendered QR code into byte[].

I would appreciate any help, Thanks

mhcuervo
  • 2,610
  • 22
  • 33
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Jan 16 '15 at 13:05
  • my problem is more how to use the zxing library properly than how to handle the exception. – Richard Fraser Vanneck Jan 16 '15 at 14:46
  • an exception without a stacktrace doesn't give any indication of the line which caused it. –  Jan 16 '15 at 14:53

1 Answers1

7

usings

using ZXing;
using Windows.UI.Xaml.Media.Imaging;

code

IBarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Height = 300,
                    Width = 300
                }
            };
var result = writer.Write("generator works");
var wb = result.ToBitmap() as WriteableBitmap;

//add to image component
image.Source = wb;

much simpler and working (tested in one of my apps)

kober
  • 832
  • 7
  • 13