In this article, we will learn about QR(Quick Response) code and how to use and generate it in Java.


QR codes are getting used almost everywhere in this technology era, where smartphones uses are getting increased day by day.
Whether you talk about movie booking apps like BookMyShow , they generate the ticket in the format of QR code
and share with you.
Have you ever used Paytm , then you must be aware that you need to scan QR code of other’s paytm wallet in order to send money.
This is popular in many apps where you scan the QR code through QR code scanner and you get redirected to web page’s URL associated with it.
Let us look at quick introduction of Quick Response (QR) codes and how to generate QR codes in Java.
What is QR code
Quick Response Code is known as QR Code. QR Code is basically a two-dimensional matrix code or barcode. QR code can be read by QR Code scanners or smartphones.
QR codes was first designed by Japan based company Denso Wave in the year 1994. It was developed by Japan mainly for their automotive industry. Denso wave created QR code to track their vehicles in their manufacturing units. Every vehicle was assigned with unique QR codes for tracking.
You can say QR codes are like barcodes but with more advantages.
QR code in compare to barcodes format can store large volume of data in very small area. barcodes can be simply replaced with QR codes at any place.
Now a days, QR codes have become more popular even outside the automotive industry as it has many advantages over barcodes
like it’s faster readability and it has huge storage capacity.
The QR code, as you can see in the sample QR code image ,is consists of black square dots arranged in square grid and it has white background.
I have created below QR Code Image for you to encode :


To create QR Codes, there are four standard modes are available – Numeric, AlphaNumeric , Byte/Binary , Kanji. Some extensions to these modes are also available for custom data.
QR Codes was designed with advantages of decoding its content in very high speed.
QR codes became popular as usage of smartphone is being increased and smartphones with QR code app can easily read the QR Codes. Now a days, we see QR codes everywhere . It can contain several information like web url, price details, product details etc.
QR Codes general usage :
- Web URL
- Location
- Credit Card information
- Movie ticket
- Authentication url
How to generate QR Code in Java
We can create and read the QR Codes in Java using below mentioned two APIs :
ZXing QR Code Generator
In Java, we can write/generate and read/parse the QR Codes using ZXing API.
ZXing API , also called as Zebra Crossing, is one popular API which can be used for QR Code processing in Java. It is an open source library and it can be used in various platforms including Java, Android, IPhone etc.
ZXing API is a multi-form 1D , 2D barcode processing library.
1) Add ZXing library or dependency in your project
For maven projects, below dependencies needs to be added in the project’s pom.xml :
<dependencies> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency> </dependencies>
Or you can simply download the library from below mentioned path and add them in your classpath :
2) Program to create QR Code in Java using ZXing
In below example, we are creating QR Code Image with text “Hello!!!!” in the location “E://HelloQRCode.jpg”.
Below methodΒ generateQRCode is taking the text, width, height, location of the image as parameter.
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerateExample { private static final String QR_CODE_LOCATION = "E://HelloQRCode.jpg"; private static void generateQRCode(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(matrix, "JPG", path); } public static void main(String[] args) { try { generateQRCode("Hello!!!!", 350, 350, QR_CODE_LOCATION); } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
If you run this program in Java, The QR code image encoded with the provided text will get store in your specified location.
Below is the snippet of QR Code image generated from my program, you can scan it and check.
The above generateQRCode method writes and saves the QR Code image in the specified location.
In case, you do not want to save the QR Code image into location but you want it to be returned as byte array, then you can useΒ ZXing’s method MatrixToImageWriter.writeToStream(). See below method :
public static byte[] generateQRCode(String text, int width, int height) throws WriterException, IOException { QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height); ByteArrayOutputStream stream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(matrix, "PNG", stream); byte[] qrCode = stream.toByteArray(); return qrCode; }
Generate QR Code using QRGen API
QRGen is also a good library to create QR Code in Java. It is simple to use. QRGen library works as a layer on top of ZXing API and makes it easy to generate QR Codes in Java.
1) Add QRGenΒ library or dependency in your project
For maven projects, below dependencies needs to be added in the project’s pom.xml :
<dependency> <groupId>net.glxn</groupId> <artifactId>qrgen</artifactId> <version>1.4</version> </dependency>
Or you can simply download the library from below mentioned path and add them in your classpath :
2) Program to create QR Code in Java using QRGen
Below example illustrates , How to create QR Code using the QRGen API.
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import net.glxn.qrgen.QRCode; import net.glxn.qrgen.image.ImageType; public class QRCodeGenerateExample { public static void main(String[] args) { ByteArrayOutputStream stream = QRCode.from("Hello!!!!") .to(ImageType.JPG).stream(); try { FileOutputStream fileStream = new FileOutputStream(new File( "E://HelloQRCode.jpg")); fileStream.write(stream.toByteArray()); fileStream.flush(); fileStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
If you run this program in Java, The QR code image encoded with the provided text will get store in your specified location.
Below is the snippet of QR Code image generated from my program, you can scan it and check.
How to secure your QR Codes
As we have seen earlier, the data which we are storing into our QR Codes i.e. the text can be easily read by any smartphone having QR Code scanner app. This is lack of security. If we want to secure your QR Code , you can do it by following below method.
QR Code does not provide any kind of security by itself but we can impose it by following below steps :
- You can encrypt the text using cryptography in Java with the secret key.
- Now store the encrypted text into your QR Code.
So Person knowing the secret key will only be able to decrypt the text. Whenever you want to use your QR Code in your application, decrypt the text with your secret key and then use it.
Conclusion
Thatβs all folks! In this article, you have got the complete overview of QR Code, Its brief history, its advantages , its uses, different APIs used for generating QR Codes in Java and steps to generate QR code in Java.