Report this

What is the reason for this report?

Java QR Code Generator - zxing example

Published on August 3, 2022
Java QR Code Generator - zxing example

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.

<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

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?

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

- Don Peter

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

- Taylor

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

- lidiya

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

- wuwang

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

- Hoa

Replying to JournalDev

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

- Pankaj

Replying to JournalDev

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.

- sarahlynnbr

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.

- Prabhat Kumar

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

- mahesh bhanushali

can you give code for qr code scanner

- mahesh bhanushali

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

- Tony

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 …

- prerna verma

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.

- Jay Dhamsaniya

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.

- bobbell2

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?

- Jay P

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?

- am0314

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

- ahmad

its very nice bro

- bala

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?

- Bell

how to encode a secret message into qr code?

- supriya

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.

- A.V.V.Y.CHAITANYA

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

- aayesha

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.

- Jay Dhamsaniya

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.

- bobbell2

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?

- Jay P

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?

- am0314

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

- ahmad

its very nice bro

- bala

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?

- Bell

how to encode a secret message into qr code?

- supriya

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.

- A.V.V.Y.CHAITANYA

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

- aayesha

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.

- Jay Dhamsaniya

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.

- bobbell2

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?

- Jay P

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?

- am0314

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

- ahmad

its very nice bro

- bala

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?

- Bell

how to encode a secret message into qr code?

- supriya

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.

- A.V.V.Y.CHAITANYA

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

- aayesha

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

- Ricardo

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

- Akash Kandwal

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

- Alz

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

- cpanon

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

- jeff

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

- Ricardo

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

- Akash Kandwal

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

- Alz

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

- cpanon

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

- jeff

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.