Java QR Code Generator

, , Leave a comment

You probably still think that barcodes are only for juice bottle labels and cereal boxes. You are wrong. Two-dimensional codes such as QR Codes can be easily scanned using smartphones and that is one of the many reasons you can see them everywhere from restaurants to magazines, business cards to store fronts.

With help of open source libraries, you can easily create your own Java QR Code. When creating a QR code with Java, you do not have to worry about limitations when it comes to the amount of information you can embed in the code. Even so, you should know that you do not have to stick to the generators provided or standard content.

Java QR Code Generator

Java QR code generator – Some open source libraries

  • ZXing: also known as zebra crossing, this is an open source library that can also generate QR codes in many different platforms like JavaSE, Android, RIM, IPhone, Symbian etc. However, when you are here, you have to generate small codes.
  • QRGen: This is also a good open source library that creates a layer above ZXing. It also makes QR Code generation Java look like kids play. QRGen has a ZXing dependency therefore; you will need QRGen along with the jar files for ZXing for you to make Java QR Code.

Java QR Code generator

Below is an example of a program you could use to make a Quick Response Code image using ZXing Api:

Package com.adly.generator;

Import java.awt.Color;

Import java.awt.Graphics2D;

Import java.awt.image.bufferedimage;

Import java.io.File;

Import java.io.ioexception;

Import java.util.Hashtable;

Import javax.imageio.imageio;

Import com.google.zxing.barcodeformat;

Import com.google.zxing.encodehinttype;

Import com.google.zxing.writerexception;

Import com.google.zxing.common.bitmatrix;

Import com.google.zxing.qrcode.qrcodewriter;

Import com.google.zxing.qrcode.decoder.errorcorrectionlevel;

Public class generateqrcode {

/**

* @param args

* @throws writerexception

* @throws ioexception

*/

Public static void main(String[] args) throws writerexception, ioexception {

String qrcodetext = "http://www.journaldev.com";

String filepath = "D:\Pankaj\JD.png";

Int size = 125;

String filetype = "png";

File qrfile = new File(filepath);

Createqrimage(qrfile, qrcodetext, size, filetype);

System.out.println("DONE");

}

Private static void createqrimage(File qrfile, String qrcodetext, int size,

String filetype) throws writerexception, ioexception {

// Create the bytematrix for the QR-Code that encodes the given String

Hashtable hintmap = new Hashtable();

Hintmap.put(encodehinttype.ERROR_CORRECTION, errorcorrectionlevel.L);

Qrcodewriter qrcodewriter = new qrcodewriter();

Bitmatrix bytematrix = qrcodewriter.encode(qrcodetext,

Barcodeformat.QR_CODE, size, size, hintmap);

// Make the bufferedimage that are to hold the qrcode

Int matrixwidth = bytematrix.getwidth();

Bufferedimage image = new bufferedimage(matrixwidth, matrixwidth,

Bufferedimage.TYPE_INT_RGB);

Image.creategraphics();

Graphics2D graphics = (Graphics2D) image.getgraphics();

Graphics.setcolor(Color.WHITE);

Graphics.fillrect(0, 0, matrixwidth, matrixwidth);

// Paint and save the image using the bytematrix

Graphics.setcolor(Color.BLACK);

For (int i = 0; i < matrixwidth; i++) {

For (int j = 0; j < matrixwidth; j++) {

If (bytematrix.get(i, j)) {

Graphics.fillrect(i, j, 1, 1);

}

}

}

Imageio.write(image, filetype, qrfile);

}

}

Java QR Code Generator 5.00/5 (100.00%) 1 vote
 

Leave a Reply

(*) Required, Your email will not be published