Generate QR code in Java

In this tutorial, we will create QR Codes for any text of our choice with the help of a program using the programming language (java). After Creating code we will scan the code using a smart-phone and display the output.

We use these codes for storing URLs or other information which we can read by scanning it with the camera. We can scan the QR code using smartphones and get the stored information.

QR Codes help us to store information in a square piece of code. We can not understand this code but we can scan the code to get proper information stored in the code.

We will download a png image of QR code and all the changes will be made on this Image when we will run our code.

How to generate QR code in Java

We can generate this code using any IDE ie. Notepad, Notepad++, Eclipse, Netbeans, etc but. In this tutorial, We are using Netbeans to generate QR Code using java because we require two jar files that we can directly add to our project using Netbeans. If we will use notepad or Notepad++ we need to add these files to our classpath and it is very time-consuming so, using Netbeans is a better option.

package javaapplication27;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class NewClass1{
        private static final String qcip = "C:\\Users\\lenovo\\Desktop\\kamal\\qrcode.png";
  private static void generateQRCode(String text, int width, int height, String filePath)
            throws Exception {
        QRCodeWriter qcwobj = new QRCodeWriter();
        BitMatrix bmobj = qcwobj.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path pobj = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bmobj, "PNG", pobj);

    public static void main(String[] args) {
        try {
            generateQRCode("My Name Is Kamalpreet singh",1250, 1250, qcip);
        } catch (Exception e) {
            System.out.println("Could not generate QR Code" + e);

javaapplication27 is the name of a package.

We require certain packages to create the QR Code. We will use ZXing ( a library used for encoding and decoding of barcode). For this, we will download 2 jar files

link to two jar files are:

java 3.0.0 jar::

JavaSE 3.3.3 jar::

NewClass1 is the name of our class.

We use Variable qcip is for storing the location of downloaded QR ( a png image).

generateQrCode() is the method for generating QR Code.We use 4 parameters with our method.

4 parameters are

  • width
  • height
  • text that we want to change
  • location of the Qr code image downloaded

We are using throws Exception for handling Exceptions in the method.

We use QRCodeWriter, Bitmatrix, and Paths to encode the message to QR Code. Only changes are made on the Existing image of the QR Code that we downloaded.

In the main method of our class, we will call the created method. In this method, we will pass the parameters(“my name is kamalpreet”, 1250,1250,qcip) 1250 and 1250 are width and height of the code

And qcip is the variable that stores the location of the QR Code image that we downloaded.

QR Code::

How to generate QR code in Java


How to generate QR code in Java

One response to “Generate QR code in Java”

  1. Tobiloba Owolabi says:

    What if I didnt want to save the QR code to a file but wanted to store it in a variable because I want to write it as part of a pdf I am generating with iText7. how do I go about that?

Leave a Reply

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