Question

Code to generate Qr code using zxing is ---

It takes string data and the imageview This works just fine

private void generateQRCode_general(String data, ImageView img)throws WriterException {
    com.google.zxing.Writer writer = new QRCodeWriter();
    String finaldata = Uri.encode(data, "utf-8");

    BitMatrix bm = writer.encode(finaldata, BarcodeFormat.QR_CODE,150, 150);
    Bitmap ImageBitmap = Bitmap.createBitmap(150, 150,Config.ARGB_8888);

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

    if (ImageBitmap != null) {
        qrcode.setImageBitmap(ImageBitmap);
    } else {
        Toast.makeText(getApplicationContext(), getResources().getString(R.string.userInputError),
                Toast.LENGTH_SHORT).show(); 
    }
}

Now my question is ,how to get bar code using the same library.i saw some files related to bar codes but i am not sure how to do it. Since I want to generate the bar code within the application and not call any web service. Since i am already using zxing,no point in including itext and barbecue jars

Was it helpful?

Solution 3

You are using QRCodeWriter. If you want to write another type of code, use another Writer.

Check this MultiFormatWriter - it can write any type of bar or find specific writers here in subfolders (this is from zxing library)

OTHER TIPS

Like Gaskoin told... MultiFormatWrite it worked :) here is the code.

      com.google.zxing. MultiFormatWriter writer =new  MultiFormatWriter();


        String finaldata = Uri.encode(data, "utf-8");

        BitMatrix bm = writer.encode(finaldata, BarcodeFormat.CODE_128,150, 150);
        Bitmap ImageBitmap = Bitmap.createBitmap(180, 40,Config.ARGB_8888);

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

        if (ImageBitmap != null) {
            qrcode.setImageBitmap(ImageBitmap);
        } else {
            Toast.makeText(getApplicationContext(), getResources().getString(R.string.userInputError),
                    Toast.LENGTH_SHORT).show(); 
        }

I've tested the accepted answer to generate a Barcode but the output is blurry when used in a big ImageView. To get a high quality output, the width of the BitMatrix, the Bitmap and the final ImageView should be the same. But doing so using the accepted answer will make the Barcode generation really slow (2-3 seconds). This happens because

Bitmap.setPixel()

is a slow operation, and the accepted answer is doing intensive use of that operation (2 nested for loops).

To overcome this problem I've modified a little bit the Bitmap generation algorithm (only use it for Barcode generation) to make use of Bitmap.setPixels() which is much faster:

private Bitmap createBarcodeBitmap(String data, int width, int height) throws WriterException {
    MultiFormatWriter writer = new MultiFormatWriter();
    String finalData = Uri.encode(data);

    // Use 1 as the height of the matrix as this is a 1D Barcode.
    BitMatrix bm = writer.encode(finalData, BarcodeFormat.CODE_128, width, 1);
    int bmWidth = bm.getWidth();

    Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, height, Config.ARGB_8888);

    for (int i = 0; i < bmWidth; i++) {
        // Paint columns of width 1
        int[] column = new int[height];
        Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE);
        imageBitmap.setPixels(column, 0, 1, i, 0, 1, height);
    }

    return imageBitmap;
}

This approach is really fast even for really big outputs and generates a high quality bitmap.

There you go,

public static Bitmap createBarCode (String codeData, BarcodeFormat barcodeFormat, int codeHeight, int codeWidth) {

    try {
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel> ();
        hintMap.put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

        Writer codeWriter;
        if (barcodeFormat == BarcodeFormat.QR_CODE) {
            codeWriter = new QRCodeWriter ();
        } else if (barcodeFormat == BarcodeFormat.CODE_128) {
            codeWriter = new Code128Writer ();
        } else {
            throw new RuntimeException ("Format Not supported.");
        }

        BitMatrix byteMatrix = codeWriter.encode (
            codeData,
            barcodeFormat,
            codeWidth,
            codeHeight,
            hintMap
        );

        int width   = byteMatrix.getWidth ();
        int height  = byteMatrix.getHeight ();

        Bitmap imageBitmap = Bitmap.createBitmap (width, height, Config.ARGB_8888);

        for (int i = 0; i < width; i ++) {
            for (int j = 0; j < height; j ++) {
                imageBitmap.setPixel (i, j, byteMatrix.get (i, j) ? Color.BLACK: Color.WHITE);
            }
        }

        return imageBitmap;

    } catch (WriterException e) {
        e.printStackTrace ();
        return null;
    }
}

Of course you can support as many BarcodeFormats as you want, just change the constructor here :

Writer codeWriter;
if (barcodeFormat == BarcodeFormat.QR_CODE) {
    codeWriter = new QRCodeWriter ();
} else if (barcodeFormat == BarcodeFormat.CODE_128) {
    codeWriter = new Code128Writer ();
} else {
    throw new RuntimeException ("Format Not supported.");
}

try this code

Context context = getActivity();
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
intent.putExtra("ENCODE_TYPE", Text);
intent.putExtra("ENCODE_DATA", "12345678901");
intent.putExtra("ENCODE_FORMAT", "UPC_A");
startActivity(intent);

hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top