Generate QR code of any URL 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.

Generate QR code 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.

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);
        }

    }
}

Output:

Leave a Comment

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

Scroll to Top