Java QR Code Generator – zxing example

Filed Under: Java

Today we will look into java QR code generator program. If you are tech and gadget savvy, then you must be aware of the QR code. 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.

Java QR code generator

When I found QR code images in so many websites then I started looking for java QR code generator. I looked into some open source APIs and found zxing to be the simple and best to use.

If you want to generate a QR code image, then we only need its core library. Just add below dependency to your maven project.


<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.3.2</version>
</dependency>

If you want to read QR image through the command line, then we need to use it’s JavaSE library. You can add below dependency for it.


<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>javase</artifactId>
	<version>3.3.2</version>
</dependency>

This will also let you know two extra dependencies required to run from the command line, as shown in the below image. We will have to add these jars into classpath to run the client app to read the QR code image. We will see this in action later on in this tutorial.

zxing maven dependencies

zxing example to generate QR code image

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

GenerateQRCode.java


package com.journaldev.qrcode.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 {

	public static void main(String[] args) throws WriterException, IOException {
		String qrCodeText = "https://www.journaldev.com";
		String filePath = "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<EncodeHintType, ErrorCorrectionLevel> 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);
	}

}

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.

java qr code generator, zxing example

zxing example to read QR code

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

Below is the command to read the QR code image file. Notice the additional jars in the classpath that zxing depends on.


$java -cp $HOME/.m2/repository/com/google/zxing/javase/3.3.2/javase-3.3.2.jar:.:$HOME/.m2/repository/com/google/zxing/core/3.3.2/core-3.3.2.jar:$HOME/.m2/repository/com/beust/jcommander/1.72/jcommander-1.72.jar:$HOME/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.3.1/jai-imageio-core-1.3.1.jar com.google.zxing.client.j2se.CommandLineRunner JD.png

Below image shows the output produced by this command.

zxing read qr code image

You can download the QR Code Generator and Reader maven project from our GitHub Repository.

Comments

  1. Akash Kandwal says:

    the qrcode not always working with the jframe
    it is showing the previous qrcode information.
    please help!

  2. Ricardo says:

    How do I change a version of QR Code? is coming out with version 2 but would like to switch to version 1 (25×25).
    thank you

  3. aayesha says:

    can u give code for eclipse,for generatin barcode and qr code using zxing core.jarand javase.jar files,,please help

  4. A.V.V.Y.CHAITANYA says:

    sir, my name is A.CHAITANYA. I am doing my master’s in IIITDM KANCHEEPURAM College. In my final year project, I am doing watermarking with QR code. So i need a verilog code to generate a QR code. So can you please send me the verilog code.
    Thank you sir.

  5. supriya says:

    how to encode a secret message into qr code?

  6. Bell says:

    Is there a way i can scan external QR codes from a PC (by way of a physically attached scanner) and capture the result/output for processing through a Java API (ZXing??) in a ‘seamless’ manner?

  7. bala says:

    its very nice bro

  8. ahmad says:

    What is the version of the qrCode if you choose the size 125?

  9. am0314 says:

    Hello, I tried to follow suit
    But import errors, I do not know how to solve

    import java.awt.Color; can not resolve symbol ‘Color’
    import java.awt.Graphics2D; can not resolve symbol ‘Graphics2D’
    import java.awt.image.BufferedImage; can not resolve symbol ‘BufferedImage’
    import javax.imageio.ImageIO; can not resolve symbol ‘ImageIO’

    Why does this happen?

  10. Jay P says:

    I want to create qr code to connect my phone to computer for presentation. May I know what things to implement in qr code besides IP address? Will above code will help to create such qr code?

  11. bobbell2 says:

    Hello,i want to generate qr code in iReport through JARenderable implementation
    with any barcode generator for java, and i need to create high quality qr code barcodes in java iReport through JARenderable implementation. This can be achieved?Can anyone give some recommendations.I would appreciate it if anyone provide me with a sample code,thanks.

  12. Jay Dhamsaniya says:

    Hi . I would like to know how to use this library of QR code generation while having multi line of string to be integrated in the QR Code. Let me know the solution for it.

    Thanks.

    1. Jay Dhamsaniya says:

      hi . i would also like to know how to read & write array into this code for array formatted data to be written on QR Code. Let me know.

      Thanks

  13. prerna verma says:

    Thank you sir for your post Generate QR code using java…..Sir in this program zxing api is used which is not built in api of jvm…would you please give me your valuable guide to how to run this program …

    1. Pankaj says:

      You will have to download zxing from https://github.com/zxing/zxing and then build it to get the latest code. Or you can add following dependency in the maven project.
      <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>2.0</version>
      </dependency>

  14. Tony says:

    Hello,

    I downloaded the axing ell from the site you posted and tried your sample code, but it appears that get_Renamed is not a method. I am using zing.monoandroid.dll. Do you know why I would not see get_Renamed?

    Any help is appreciated!!

  15. mahesh bhanushali says:

    can you give code for qr code scanner

  16. mahesh bhanushali says:

    can u mail me code for qr code encode and decode for android

  17. Prabhat Kumar says:

    i want to get a text from jtextfirld and show the QR code as a jlabel when a button is pressed. Please provide me java code for it.

  18. Hoa says:

    are there any java lib to embedded image into qr code?

    1. Pankaj says:

      I didn’t understood what you are trying to achieve.

    2. sarahlynnbr says:

      I searched online and found that most java barcode generators only help you create a standard QR Code, some might even allow you to change the color of it. But most don’t support adding images inside of it. You can only add this after the barcode is created with some other program like photoshop, etc.

  19. wuwang says:

    how to control the qrcode image size 0.74cm *0.74cm

  20. lidiya says:

    hi
    can u please guide me in developing a color 2D barcode..?

  21. Taylor says:

    I watched the passage which provides a qr code Java class library to create qr code image in android application. It might be helpful!

  22. Don Peter says:

    Hi
    I was wondering if you could guide me in creating the Qrcode in android without using zxing.
    The problem with porting the above application is that awt and javax libraries are not compatible in android

    1. Pankaj says:

      I am not fully aware of how it can be done in android application but if you want, you can use dynamic generation using google chart API.

    2. Malik Sanawer says:

      I’m android developer and I’ve complete code to scan and generate QR code as well as barcode using java in your android application . If anybody interested to want help regarding this then mail me freely.
      sanawermalik@gmail.com

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages