locked
Zxing to generate barcode in Windows Store apps

    Question

  • How can I use Zxing.net library for generating bar codes in Windows Store apps? Here is a code bit that I used to try: 

    using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
                {
                     var code = cardNumber.Text;
                     var wr = new BarcodeWriter { Format = BarcodeFormat.CODE_128, Options = new ZXing.Common.EncodingOptions { PureBarcode = true, Width = 500, Height = 122 } };
                     var bytes = wr.Write(code);
                     var byteArray = bytes.Pixel;
                     BitmapImage image = new BitmapImage();
                     image.SetSource(ms);
                     img.Source = image;
                }
    Monday, April 21, 2014 6:35 AM

Answers

  • Wouldn't it be better to ask ZXing?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 21, 2014 1:06 PM
    Moderator

All replies

  • Wouldn't it be better to ask ZXing?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 21, 2014 1:06 PM
    Moderator
  • hi,

    I'm not familiar with Zxing barcode ,but I do have an experience with Spire.Barcode and Onbarcode.

    They're both nice barcode components and allow programmer to develope barcode device on any .NET platform.

    http://freebarcode.codeplex.com/

    http://www.onbarcode.com/

    You can donwload the trail version and evaluate by yourself.feel free to ask me if any questions.

    • Proposed as answer by Joseph Orjinta Tuesday, December 16, 2014 5:11 PM
    • Unproposed as answer by Joseph Orjinta Tuesday, December 16, 2014 5:12 PM
    Wednesday, April 30, 2014 8:03 AM
  • You can use to function to generate Byte[] value of QR or BarCode, then you then

    set it to your image source, without option to resize the code snippet - LoadImage Function.

    public async Task<byte[]> GenerateQR(int width, int height, string text)

    {  
    //Generate QRCode

    var bw = new ZXing.BarcodeWriter();


    var encOptions = new ZXing.Common.EncodingOptions() { Width = width, Height = height, Margin = 0 };

        bw.Options = encOptions;

        bw.Format = ZXing.BarcodeFormat.QR_CODE;

    ZXing.Rendering.PixelData obj =  bw.Write(text);


    //  Write the pixels to a InMemoryRandomAccessStream


    var stream = new  InMemoryRandomAccessStream();

          
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);


    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)obj.Width, (uint)obj.Heigth, 96, 96,obj.Pixel);

           
    await encoder.FlushAsync();
     stream.Seek(0);

     

    var reader = new DataReader(stream.GetInputStreamAt(0));

     
    var bytes = new byte[stream.Size];

    await reader.LoadAsync((uint)stream.Size);

     reader.ReadBytes(bytes);

    return bytes;

    }

    The function for loading Image to your Image Control based text value

    private async Task LoadImage(string text)
    {
        WriteableBitmap PicWriteableBitmap = new WriteableBitmap(100, 100);
         MemoryStream ms = new MemoryStream(GenerateQR(100,100,text);

                          using (IRandomAccessStream fileStream = ms.AsRandomAccessStream())// file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                          {
                              BitmapDecoder decoder = await BitmapDecoder.CreateAsync(Windows.Graphics.Imaging.BitmapDecoder.JpegDecoderId, fileStream);

                           

                              // Scale image to appropriate size
                              BitmapTransform transform = new BitmapTransform()
                              {
                                  ScaledWidth = 100,
                                  ScaledHeight = 100
                              };

                              //PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
                              PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                                  BitmapPixelFormat.Bgra8,    // WriteableBitmap uses BGRA format
                                  BitmapAlphaMode.Straight,
                                  transform,
                                  ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation
                                  ColorManagementMode.DoNotColorManage);

                              // An array containing the decoded image data, which could be modified before being displayed
                              byte[] sourcePixels = pixelData.DetachPixelData();

                              // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer
                              using (Stream stream = PicWriteableBitmap.PixelBuffer.AsStream())
                              {
                                  await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length);
                              }
                          }
                        PicWriteableBitmap.Invalidate();

       //Control for displaying image
                        imgImage.Source = PicWriteableBitmap;

    }

    Hope this works for you.

    Tuesday, December 16, 2014 5:29 PM