Generate QR Code image from Java Program

If you are tech and gadget savvy, then you must be aware of QR codes. You will find it everywhere these days – in blogs, websites and even in some public places. This is very popular in mobile apps, where you scan the QR code using a QR Code scanner app and it will show you the text or redirect you to the web page if it’s URL. I came across this recently and found it very interesting. If you want to know about QR Code, you can find a lot of useful information at Wikipedia QR Code Page.

When I found these kind of images in so many websites then I started looking how to generate it using Java Code. I looked into some APIs available as open source in the market and found zxing to be the simplest and best to use.

Here is the program you can use to create QR Code image with zxing API.

package com.adly.generator;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import javax.imageio.ImageIO;


public class GenerateQRCode {

	 * @param args
	 * @throws WriterException
	 * @throws IOException
	public static void main(String[] args) throws WriterException, IOException {
		String qrCodeText = "";
		String filePath = "D:\\Pankaj\\JD.png";
		int size = 125;
		String fileType = "png";
		File qrFile = new File(filePath);
		createQRImage(qrFile, qrCodeText, size, fileType);

	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,

		Graphics2D graphics = (Graphics2D) image.getGraphics();
		graphics.fillRect(0, 0, matrixWidth, matrixWidth);
		// Paint and save the image using the ByteMatrix

		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);


Here is the QR Code image file created by this program. You can use your mobile QR Code scanner app to test it. It should point to JournalDev Home URL.

If you don’t have a mobile app to test it, don’t worry. You can test it with zxing API through command line too.

I am on Windows OS and here is the command to test it. If you are on Unix/Linux/Mac OS then change it accordingly.

D:\Pankaj\zxing>java -cp javase\javase.jar;core\core.jar D:\Pankaj\JD.png
file:/D:/Pankaj/JD.png (format: QR_CODE, type: URI):
Raw result:
Parsed result:
Found 4 result points.
  Point 0: (35.5,89.5)
  Point 1: (35.5,35.5)
  Point 2: (89.5,35.5)
  Point 3: (80.5,80.5)

Tip for Dynamic QR Code Generation

If you want to generate QR code dynamically, you can do it using Google Charts Tools.
For above scenario, URL will be×125&cht=qr&chl=

Guest Post by PartyPoker

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to JournalDev Newsletter
Get the FREE access to JournalDev Newsletter and Free PDF eBooks
*No Spam Guaranteed. By entering your email address, you agree also subscribing to our newsletter.
Oops! - Something went wrong.