Trouble generating Barcode using ZXing library with large data

6.6k views Asked by At

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

2

There are 2 answers

3
BrentM On BEST ANSWER

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.

0
Gowtham On

https://play.google.com/store/apps/details?id=com.srowen.bs.android android app scans perfectly. But while generating barcode make sure it's less width and height as possible, to easily fit to mobile camera to scan.

I regenerated barcode of CODE-128 for 900000588548001100001305000000000207201512345.6|12345.7 with just 300 and 150 it's able to scan, if it's not working for you then can scale up width and height. Barcode generated: https://i.stack.imgur.com/UF0qJ.png

Screenshot after scanning