Search code examples
javaandroidimagebarcodezxing

Trouble generating Barcode using ZXing library with large data


I need to generate barcode in 128A format with data: 900000588548001100001305000000000207201512345.6|12345.7

I'm using ZXing library and Here is my method:

private void barcodeGenerator(String data)
{
    try
    {
        com.google.zxing.MultiFormatWriter writer = new MultiFormatWriter();

        BitMatrix bm = writer.encode(data, BarcodeFormat.CODE_128, 700, 200);
        Bitmap ImageBitmap = Bitmap.createBitmap(700, 200, Config.ARGB_8888);

        for (int i = 0; i < 700; i++)
        {//width
            for (int j = 0; j < 200; j++)
            {//height
                ImageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
            }
        }

        File f = new File(Environment.getExternalStorageDirectory() + "/barcode1.png");


        FileOutputStream fos = new FileOutputStream(f);
        ImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

This method generates and stores the barcode image in SDCard which m scanning using ZXing Barcode Scanner.

Barcode is successfully scanned when data is small. eg: 123.4|456.7

enter image description here

But if data is large, eg: 900000588548001100001305000000000207201512345.6|12345.7

It looks like some wrong barcode is generated and Scanner is not able to scan the generated barcode.

enter image description here

Thanks in advance for help.

Edit: Have added generated barcode images


Solution

  • You can upload the barcode image you produced to the ZXing Decoder Online to confirm if it is valid:

    http://zxing.org/w/decode.jspx

    There is no intrinsic limit to the length of a Code 128 barcode, and all the characters you have are valid.

    Having said that, with Code 128A barcodes, having more than 20 characters encoded makes the resultant barcode very wide and hard to scan.

    It is likely that the barcode is valid but the scanners camera is not able to get a clear enough picture of such a large barcode.

    Take a look at this question for more information: Unable to scan Code 128

    If possible, it would be recommended to use an alternative barcode format, such as QR code, which can store more data without increasing the barcode size.