Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Generate QR code and Barcode using Zxing

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

like image 992
Ciff Avatar asked Mar 13 '14 07:03

Ciff


People also ask

Can ZXing scan QR code?

On click of button_scan_qr_code , CaptureActivity will start scanning using default camera. Once it scans any QR code, it sends back the result to onActivityResult the MainActivity . ZXing also provides online QR Code Generator. Enter the required fields, generate and scan it to get the results.

How do I use ZXing library on Android?

In order to use the Zxing library in our application we need to add it's dependency in our application's gradle file. For adding the dependency Go to Gradle Scripts > build. gradle(Module: app) and add the following dependencies. After adding the dependency you need to click on Sync Now.


1 Answers

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(); 
        }
like image 64
Ciff Avatar answered Sep 28 '22 04:09

Ciff