Generate QR code in Java

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

We use QR codes for storing URLs or other information which we can read by scanning it with a camera. We can scan the QR code using smartphones and get the stored information. QR codes store information in a square piece of image. It gives the output image in any standard format such as PNG/JPG/JPEG etc. in the form of QR code when we run the program.

How to generate QR code for any text in Java

We can generate this code using any IDE i.e., IntelliJ IDEA, Notepad++, NetBeans, etc. In this tutorial, we are using IntelliJ IDEA to generate QR code using Java because we require two jar files that we can directly add to our project using IntelliJ IDEA.

If we use Notepad or Notepad++ then we need to add these files to our CLASSPATH and it is very time-consuming so, using IntelliJ IDEA is a better option.

Java Program: Text to QR Code Converter

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class NewClass1 {

    //Output directory of the QR Code will be stored at qcip
    private static final String qrOutput = "C://Users//Administrator//Desktop//QR Code.JPEG";


    private static void generateQRCode(String text, int width, int height, String filePath)
            throws Exception {
        QRCodeWriter qrCodeWriterObj = new QRCodeWriter();
        BitMatrix bitMatrixObj = qrCodeWriterObj.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path pathObj = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrixObj, "JPEG", pathObj);
    }

    public static void main(String[] args) {
        try {
            generateQRCode("Thank you for using CodeSpeedy", 1250, 1250, qrOutput);
        } catch (Exception e) {
            System.out.println("Could not generate QR Code" + e);
        }

    }
}

We require certain packages to create the QR code. We will use ZXing (a library used for encoding and decoding of bar code). For this, we will download two jar files and the link to the jar files are:

java 3.3.0 jar ::https://mvnrepository.com/artifact/com.google.zxing/core/3.3.0

JavaSE 3.3.3 jar ::https://mvnrepository.com/artifact/com.google.zxing/javase/3.3.3

We use variable qrOutput for storing the location of downloaded output of the QR image(QR code image name can be defined by the user in the code).

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

4 parameters are

  • width
  • height
  • text that we want in the form of QR code
  • location of the QR code image downloaded/output

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

We use QRCodeWriter, Bitmatrix and Paths to encode the message to QR code.

A new file is created in the form of a QR code using this method in the output location(qrOutput). In the main method of our class, we will call the created method. In this method, we will pass the parameters(“Thank you for using CodeSpeedy”, 1250, 1250, qrOutput) where 1250 and 1250 are the width and height of the QR code image which again can be customized by the user in the code and the QR code image will adjust its resolution accordingly.

QR CODE OUTPUT:

Generate QR code for any text in Java

TEXT OUTPUT FROM QR:

QR code java

Related Posts

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 *