Search code examples
c#barcode

C# Created Barcodes Scanning Different Values Than Those Encoded


I am working on a C# program that creates and displays UPCA barcodes (using the NuGet package BarCodeLib). These barcodes seem to work perfectly, until you scan them at least. When they are scanned, I've seen the scanned number be up to 3 more or less than the encoded value. Sometimes they're 2 off, sometimes 1 off, and rarely they actually match. Could this be an image size issue? Could a barcode being disproportionately tall or wide cause this issue?

public string barcodePath
        {
            get
            {
                try
                {
                    string dir = System.Configuration.ConfigurationManager.AppSettings["BarcodeDirectory"];
                    System.IO.Directory.CreateDirectory(dir);
                    BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                    string oid = recipientId.ToString();
                    char[] array = oid.ToCharArray();
                    int end = 12 - oid.Length;
                    for (int i = 0; i < end; i++)
                    {
                        oid = "0" + oid;
                    }

                    Image img = b.Encode(BarcodeLib.TYPE.UPCA, oid, Color.Black, Color.White, 290, 201); //290, 120
                    string imgPath = dir + @"\" + recipientId + ".jpg";
                    img.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    return "file:///" + imgPath;
                }
                catch (Exception ex)
                {
                    return "";
                }
            }

These two barcodes were created sequentially within a loop.

48989 - Scans fine 48989

48990 - Scans to 48996 enter image description here


Solution

  • What the library is doing is correcting the final digit of your 12 digit, zero padded number.

    See this comment from http://codeproject.com/Articles/20823/Barcode-Image-Generation-Library "◦The UPC-A check digit is now calculated every time whether 11 or 12 digits are passed in. If 12 is passed in and it has got an incorrect check digit, then it is replaced with the correct check digit. This prevents an unscannable barcode from being generated."

    The correct check digit for "00000004898" is 9. For "00000004899" it is 6.

    You could just zero-pad your number to 11 digits & allow the library to add the correct checksum value - or do it yourself - see : http://www.makebarcode.com/specs/upc_a.html