Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode a string in a QR code using zxing on Android?

I am trying to generate a QR code using Zxing on Android. Since java.awt.image.BufferedImage is not included in Android, I am not sure how to create the QR code image on Android.

EDIT: I don't want my application to require internet access.

like image 258
user477519 Avatar asked Jan 14 '12 01:01

user477519


2 Answers

I found this to be a helpful http://codeisland.org/2013/generating-qr-codes-with-zxing/

Using the above resource, I created a utility method as follows:

public static Bitmap encodeToQrCode(String text, int width, int height){
    QRCodeWriter writer = new QRCodeWriter();
    BitMatrix matrix = null;
    try {
        matrix = writer.encode(text, BarcodeFormat.QR_CODE, 100, 100);
    } catch (WriterException ex) {
        ex.printStackTrace();
    }
    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, matrix.get(x,y) ? Color.BLACK : Color.WHITE);
        }
    }
    return bmp;
}
like image 133
Stephen Paul Avatar answered Oct 03 '22 07:10

Stephen Paul


Please look at the zxing source code under android/. In the .encode package you will see an example of exactly how to encode a QR code in Android. Yes, you can't use, and don't need to use BufferedImage.

like image 23
Sean Owen Avatar answered Oct 03 '22 06:10

Sean Owen