2

I have added the Zxing.Net from NuGet Package Manager in VS 2015. I tried the following code for decoding a CODE_128 barcode. But its giving null as result. The same image is getting decoded successfully in almost all the online barcode reading websites including Zxing Online Decoder.

using System;
using System.Drawing;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

public string barcode_scan()
{
    string qr = @"C:\Users\Admin\Desktop\barcode.jpg";
    ZXing.BarcodeReader reader = new ZXing.BarcodeReader();
    var result = reader.Decode((Bitmap)Bitmap.FromFile(qr));
    return result;
}

I'm not able to make out where I'm going wrong.

Edit: Image Attached Image with barcode

Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38

2 Answers2

2

Have you tried with:

ZXing.BarcodeReader reader = new ZXing.BarcodeReader()
{
    AutoRotate = true,
    TryInverted = true,
    Options = new DecodingOptions
    {
        TryHarder = true,
        PureBarcode = true,
        PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.CODE_128 }
    }
};

This won't be optimized for speed, but if it works, you can remove some of the brute-force options.

Patrick
  • 5,526
  • 14
  • 64
  • 101
  • In case I want to decode any barcode which is given as input?? Should I remove that `BarcodeFormat.CODE_128` ?? – Jibin Balachandran Apr 26 '16 at 08:30
  • Your comment specifically mentioned `CODE_128`, so I gave you that. There are other options for formats, that can be added to the `List<>`. – Patrick Apr 26 '16 at 08:31
  • 1
    Then either the .Net port of this is not working and you'd need to submit an issue to the project, or provide us with the image you are attempting to use so something can be tested. – Patrick Apr 26 '16 at 08:36
  • I have attached the image – Jibin Balachandran Apr 26 '16 at 08:39
  • Don't use the option "PureBarcode = true". That one is only introduced for black-white Images which are generated by the encoder. It does generally not work with real life images. Btw. the barcode can be successfully decoded with the WinForms sample application of the ZXing.Net Project. You need the option "TryHarder = true". – Michael Apr 27 '16 at 05:59
1

If you crop out a piece of the image, the barcode is decoded correctly. Apparently zxing cannot determine that the "MDS" barcode is the barcode you're trying to scan.

Simply removing the EAN13 from the image is not enough, but if you have an image with just the vertical "elephant bar" it does find the barcode:

enter image description here

In other words, you'll need to "aim" the scanner :)

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72