Java QR Code Generator – zxing example

Filed Under: Java

Today we will look into the 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 on so many websites, 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.


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.


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.

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

	public static void main(String[] args) throws WriterException, IOException {
		String qrCodeText = "";
		String filePath = "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<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);

		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.

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 JD.png

The 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.


  1. jeff says:

    Hi I PUT THE QR code in inclipse it says done but i dont know where the image is stored. it says jd.png where is that. thanks

  2. cpanon says:

    Very well done. Other zxing code produces errors. Thank you.

  3. Alz says:

    I got an error from line : image.createGraphics(); while running in linux server.

    Caused by: java.awt.AWTError: Can’t connect to X11 window server using ‘:0.0’ as the value of the DISPLAY variable.

  4. Akash Kandwal says:

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

  5. 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

  6. aayesha says:

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

  7. 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.

  8. supriya says:

    how to encode a secret message into qr code?

  9. 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?

  10. bala says:

    its very nice bro

  11. ahmad says:

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

    1. Manoj says:

      QRCode Version 27

  12. 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?

  13. 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?

  14. 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.

  15. 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.


    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.


  16. 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 and then build it to get the latest code. Or you can add following dependency in the maven project.

  17. Tony says:


    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!!

  18. mahesh bhanushali says:

    can you give code for qr code scanner

  19. mahesh bhanushali says:

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

  20. 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.

  21. 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.

  22. wuwang says:

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

  23. lidiya says:

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

  24. 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!

  25. Don Peter says:

    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.

      1. chinomso George says:

        pls need a help to accept multiple information in qr code

      2. Harish says:

        Hi once scanned a qr code , i need to navigate to next screen based on result. let me know how navigate to next screen once qr code is scanned.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors