Search code examples
c#barcodezxingbarcode-scanner

ZXing barcode scanner for .NET


I am trying to use this library https://zxingnet.codeplex.com/ . Images are successfully decoded on this site https://zxing.org/w/decode.jspx, but not in my code.

Here are two ways that I have tried:

BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true, TryInverted = true, PossibleFormats = fmts };
Result result = reader.Decode(new Bitmap(@"D:\\6.jpg"));

and:

public static byte[] ImageToByte(Bitmap img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

MultiFormatReader m_BarcodeReader = new MultiFormatReader();
var hints = new Dictionary<DecodeHintType, object>();
var fmts = new List<BarcodeFormat>();
fmts.Add(BarcodeFormat.EAN_13);
hints.Add(DecodeHintType.TRY_HARDER_WITHOUT_ROTATION, false);
hints.Add(DecodeHintType.POSSIBLE_FORMATS, fmts);

Result rawResult;
Bitmap image = new Bitmap(@"D:\\6.jpg");
RGBLuminanceSource r = new RGBLuminanceSource(ImageToByte(image), image.Width, image.Height);
GlobalHistogramBinarizer x = new
HybridBinarizer(r);
BinaryBitmap bitmap = new BinaryBitmap(x);
try
{
    rawResult = m_BarcodeReader.decode(bitmap, hints);

    if (rawResult != null)
    {
        return rawResult.Text;
    }
}
catch (ReaderException e)
{

}

In both cases the result of decoding is null. What am I doing wrong here? Here is the sample image:

enter image description here


Solution

  • I finally made a total restart as is was not working as expected.

    I implemented the following algorythm: If the decoder doesn't read the barcode, split the image in 4 and restart.

    It worked pretty well after and I think this is how the website you mentionned is working. Too bad it doesn't use this method from scratch.

    note: this code is far from perfect, makes a lot of assumptions, if you copy it and use it as is, it may crash if your image is not in the same format that the one provided by the OP

    internal class Program
    {
        private static readonly List<BarcodeFormat>  Fmts = new List<BarcodeFormat> { BarcodeFormat.All_1D };
    
        static void Main(string[] args)
        {
            Bitmap originalBitmap = new Bitmap(@"C:\Users\me\Desktop\6.jpg");
            Bitmap img = CropImage(originalBitmap, new Rectangle(0 , 0, originalBitmap.Width, originalBitmap.Height));
            int width = img.Width;
            int heigth = img.Height;
            int nbOfFrames = 1;
            bool found = false;
            while (!found && width > 10 && heigth > 10)
            {
                if (DecodeImg(img))
                {
                    break;
                }
                nbOfFrames *= 4;
                width /= 2;
                heigth /= 2;
                var x = 0;
                var y = 0;
    
                for (int i = 0; i < nbOfFrames; i++)
                {
                    img.Dispose();
                    img = new Bitmap(CropImage(originalBitmap, new Rectangle(x, y, width, heigth)));
    
                    if (DecodeImg(img))
                    {
                        found = true;
                    }
                    x += width;
                    if (x < originalBitmap.Width)
                    {
                        continue;
                    }
                    x = 0;
                    y += heigth;
                    if (y >= originalBitmap.Height)
                    {
                        y = 0;
                    }
                }
            }
    
    
        }
    
        public static Bitmap CropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            return bmpImage.Clone(cropArea, PixelFormat.Format24bppRgb);
        }
    
        public static bool DecodeImg(Bitmap img)
        {
            BarcodeReader reader = new BarcodeReader
            {
                AutoRotate = true,
                TryInverted = true,
                Options =
                {
                    PossibleFormats = Fmts,
                    TryHarder = true,
                    ReturnCodabarStartEnd = true,
                    PureBarcode = false
                }
            };
            Result result = reader.Decode(img);
    
            if (result != null)
            {
                Console.WriteLine(result.BarcodeFormat);
                Console.WriteLine(result.Text);
                return true;
            }
    
            Console.Out.WriteLine("Raté");
            return false;
        }
    }