2

Does anyone knows how to generate QR code using Java program? I need to make an application to generate QR code for given details to Android device. Thank you!

senps
  • 584
  • 2
  • 10
  • 30

2 Answers2

4

Try ZebraCrossing (ZXing), it looks good: http://code.google.com/p/zxing/

String contents = "Code";
BarCodeFormat barcodeFormat = BarCodeFormat.QR_CODE;

int width = 300;
int height = 300;

MultiFormatWriter barcodeWriter = new MultiFormatWriter();
BitMatrix matrix = barcodeWriter.encode(contents, barcodeFormat, width, height);
BufferedImage qrCodeImg = MatrixToImageWriter.toBufferedImage(matrix);
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

Check the below code using QRGen an api for java built on top ZXING

private Bitmap generateQRCodeFromText(String text) {
        return QRCode.from(text)
                .withSize(QR_CODE_SIZE, QR_CODE_SIZE)
                .bitmap();
    }

OR

private Bitmap generateQRCodeFromText(String text){
    try {
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE);
        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);
            }
        }
        return bmp;

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

Or using zxing-android-embedded

 private Bitmap generateQRCodeFromText(String text) {
     try {
      BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
      Bitmap bmp= barcodeEncoder.encodeBitmap("content", BarcodeFormat.QR_CODE, 400, 400);
       return bmp;
    } catch(Exception e) {
       e.printStackTrace();
    }
    return null;
}
MohamedHarmoush
  • 1,033
  • 11
  • 17