102

I need to create a qrcode in my android application, and I need a library or source code that lets me create a QR Code in an Android app.

The library I need must:

  1. not leave a watermark (like onbarcode library)
  2. not use web service API to create the qrcode (like Google's library zxing)
  3. not need 3rd party installers (like QR Droid)

I already created such code for iPhone (Objective-C) but I need a quick fix for Android until I have time to make a QR Code generator of my own. It's my first android project so any help will be appreciated.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
Radu
  • 3,434
  • 4
  • 27
  • 38

6 Answers6

109

with zxing this is my code for create QR

 QRCodeWriter writer = new QRCodeWriter();
    try {
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        ((ImageView) findViewById(R.id.img_result_qr)).setImageBitmap(bmp);

    } catch (WriterException e) {
        e.printStackTrace();
    }
Stefano
  • 3,127
  • 2
  • 27
  • 33
79

Have you looked into ZXING? I've been using it successfully to create barcodes. You can see a full working example in the bitcoin application src

// this is a small sample use of the QRCodeEncoder class from zxing
try {
    // generate a 150x150 QR code
    Bitmap bm = encodeAsBitmap(barcode_content, BarcodeFormat.QR_CODE, 150, 150);

    if(bm != null) {
        image_view.setImageBitmap(bm);
    }
} catch (WriterException e) { //eek }
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Rob
  • 7,039
  • 4
  • 44
  • 75
  • I have forked the bitcoin open source from github and tryout on my android 2.2 devices, application force closed, anything wrong? – Roy Lee Mar 25 '13 at 01:53
  • Sorry I haven't looked at that app for over a year. As mentioned below by Sean, checkout the source for the zxing encoder here: http://code.google.com/p/zxing/source/browse/trunk#trunk/core/src/com/google/zxing/qrcode/encoder – Rob Mar 25 '13 at 11:06
  • @Rob it will generate QR code of user defined number ? only number or number with laphabets – Erum Jan 26 '15 at 12:58
  • @Erum it will create a barcode for any most alpha numeric strings you provide – Rob Aug 28 '15 at 15:26
  • Use this: implementation 'me.dm7.barcodescanner:zxing:1.9.13' – M. Usman Khan Nov 06 '19 at 06:14
52

Maybe this old topic but i found this library is very helpful and easy to use

QRGen

example for using it in android

 Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
Antwan
  • 3,837
  • 9
  • 41
  • 62
21

Here is my simple and working function to generate a Bitmap! I Use ZXing1.3.jar only! I've also set Correction Level to High!

PS: x and y are reversed, it's normal, because bitMatrix reverse x and y. This code works perfectly with a square image.

public static Bitmap generateQrCode(String myCodeText) throws WriterException {
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage

    QRCodeWriter qrCodeWriter = new QRCodeWriter();

    int size = 256;

    ByteMatrix bitMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
    int width = bitMatrix.width();
    Bitmap bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < width; y++) {
            bmp.setPixel(y, x, bitMatrix.get(x, y)==0 ? Color.BLACK : Color.WHITE);
        }
    }
    return bmp;
}

EDIT

It's faster to use bitmap.setPixels(...) with a pixel int array instead of bitmap.setPixel one by one:

        BitMatrix bitMatrix = writer.encode(inputValue, BarcodeFormat.QR_CODE, size, size);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = bitMatrix.get(x, y) ? BLACK : WHITE;
            }
        }

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
Blenest
  • 3
  • 2
bluestart83
  • 317
  • 3
  • 6
  • This code works fine except that you have to include this (or higher) __implementation 'com.google.zxing:core:3.2.0'__ on the __dependencies__ at the __build.gradle(:app)__ and the object __writer.encode(...)__ needs to be __qrCodeWriter.encode(...)__ as was defined in the original non-edit; or change it to __writer__. I hope this adds some value. – Ajowi Aug 11 '21 at 13:45
13

I used zxing-1.3 jar and I had to make some changes implementing code from other answers, so I will leave my solution for others. I did the following:

1) find zxing-1.3.jar, download it and add in properties (add external jar).

2) in my activity layout add ImageView and name it (in my example it was tnsd_iv_qr).

3) include code in my activity to create qr image (in this example I was creating QR for bitcoin payments):

    QRCodeWriter writer = new QRCodeWriter();
    ImageView tnsd_iv_qr = (ImageView)findViewById(R.id.tnsd_iv_qr);
    try {
        ByteMatrix bitMatrix = writer.encode("bitcoin:"+btc_acc_adress+"?amount="+amountBTC, BarcodeFormat.QR_CODE, 512, 512);
        int width = 512;
        int height = 512;
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (bitMatrix.get(x, y)==0)
                    bmp.setPixel(x, y, Color.BLACK);
                else
                    bmp.setPixel(x, y, Color.WHITE);
            }
        }
        tnsd_iv_qr.setImageBitmap(bmp);
    } catch (WriterException e) {
        //Log.e("QR ERROR", ""+e);

    }

If someone is wondering, variable "btc_acc_adress" is a String (with BTC adress), amountBTC is a double, with, of course, transaction amount.

Adam Staszak
  • 1,674
  • 14
  • 16
5

zxing does not (only) provide a web API; really, that is Google providing the API, from source code that was later open-sourced in the project.

As Rob says here you can use the Java source code for the QR code encoder to create a raw barcode and then render it as a Bitmap.

I can offer an easier way still. You can call Barcode Scanner by Intent to encode a barcode. You need just a few lines of code, and two classes from the project, under android-integration. The main one is IntentIntegrator. Just call shareText().

MilapTank
  • 9,988
  • 7
  • 38
  • 53
Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • tanks this seems useful, only i'd hope to bypass any web API's, that might a bit unconfortable, in offline mode for my app, but this is very promising, i'll look into it – Radu Jan 10 '12 at 16:15
  • Not sure I was clear but I am saying that the zxing options do *not* involve any web APIs – Sean Owen Jan 10 '12 at 16:49
  • Nope, it uses some libraries unavaile on android, like java.awt – siemanko Jan 09 '13 at 21:37
  • Hello, sorry to necro this thread, I think it's better if I just asked a question here. I have the Barcode Scanner Application by you guys (I'm using it to scan a QR Code) and if I got this answer right, I could just do the same, open the Barcode Scanner via intent with different arguments and it would return a Bitmap QR Code for display? – Razgriz Feb 14 '13 at 11:00
  • 1
    @nivwusquorum, no, the encoder does not use `java.awt`, or connect to the web, and you can see it used in the Barcode Scanner application -- on Android, without an internet connection. @Razgriz no it does not return the image, but shows the image on screen for you. The user can save the image. – Sean Owen Feb 14 '13 at 11:15
  • That's good enough for me. How do I call the Barcode Scanner via intent to show a QR Code? – Razgriz Feb 14 '13 at 12:15
  • http://code.google.com/p/zxing/wiki/ScanningViaIntent The example shows scanning, but the class it refers to has a method to encode too. – Sean Owen Feb 14 '13 at 12:33
  • I tried the following code, and I'm given a "Could not encode a barcode from the data provided." `Intent intent = new Intent("com.google.zxing.client.android.ENCODE");` `intent.setPackage("com.google.zxing.client.android");` `intent.putExtra("TEXT", "12321313231");` `startActivity(intent);` – Razgriz Feb 15 '13 at 03:11
  • Oh nevermind I fixed it. I used this: http://code.google.com/p/zxing/wiki/ScanningViaIntent – Razgriz Feb 15 '13 at 04:11
  • Last one. Is there a way to close the activity programmatically? For example, once the device displaying the QR Code gets a certain message (via bluetooth), it will close the activity showing the QR Code? – Razgriz Feb 15 '13 at 14:52
  • @Razgriz nope. Since the Activity showing the QR is outside of your app (it is actually the BarcodeScanner application) your app will have no control over it. By default on android all applications run "sandboxed" so that one cannot mess with another. It used to be possible to kill other applications, but the newer versions of android do not allow it. – FoamyGuy Feb 15 '13 at 22:20