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.Martijn Courteaux– Martijn Courteaux2011-11-26 17:47:17 +00:00Commented Nov 26, 2011 at 17:47
-
1No problem. But if I changed it myself, next time you would write it again like JAVA. Telling the people works better. :)Martijn Courteaux– Martijn Courteaux2011-11-26 17:51:42 +00:00Commented Nov 26, 2011 at 17:51
-
@MartijnCourteaux: Pet peeve?Kevin Coppock– Kevin Coppock2011-11-26 18:54:13 +00:00Commented Nov 26, 2011 at 18:54
Add a comment
|
2 Answers
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);
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;
}