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!

3
  • NO IT IS NOT JAVA. It's Java. Commented Nov 26, 2011 at 17:47
  • 1
    No problem. But if I changed it myself, next time you would write it again like JAVA. Telling the people works better. :) Commented Nov 26, 2011 at 17:51
  • @MartijnCourteaux: Pet peeve? Commented Nov 26, 2011 at 18:54

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.! I'll have a look. :)
BufferedImage does not seem to be available in the Android SDK.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.