Android Generate QR code and Barcode using Zxing

Ciff picture Ciff · Mar 13, 2014 · Viewed 25.6k times · Source

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

Answer

Ciff picture Ciff · Mar 13, 2014

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(); 
        }