QR Code to Text converter in Java

In this tutorial, we will generate a code that will give us the information stored in the QR Code as output using Java. We can say that we will write code that will decode the QR Code.

QR Code refers to the image which contains information stored in it in the forms of the square piece of code. It is impossible for us to read the information contained by the QR Code directly without using any scanner. We generally use smartphones to read the QR Code. We scan the code using an application to get the stored data as our output.

We will save the image of the QR Code on our computer and will pass the path of the image to be scanned.

How To Generate Text from QR Code using Java

We can generate our code using any IDE ie. Notepad, Notepad++, Netbeans, Eclipse, etc. But we will use Netbeans or Eclipse because we require jar file ie.java 3.0.0 jar and JavaSE 3.3.3 jar which we can directly add to Libraries of Netbeans or Path of Eclipse. But it is not the same in case of Notepad and Notepad++ as we need to add jar files to the classpath of our computer to run our project via Notepad, Notepad++. And we can say that for writing java programs Netbeans and Eclipse are the best IDE’s.

Link to download the jar files

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

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

We can not run our program without these jar files. But we can use other jar files instead of these jar files. We used these jar files because these are easy to use and locate.

 

Java program: QR Code to Text converter

 

package javaapplication29;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 import javax.imageio.ImageIO;
 import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class NewClass {
    public static void main(String args[])throws IOException, NotFoundException{

File fileobj = new File("C:\\Users\\lenovo\\Desktop\\kamal\\qrcode.png");
 
    BufferedImage bfrdImgobj = ImageIO.read(fileobj);
    LuminanceSource source = new BufferedImageLuminanceSource(bfrdImgobj);
    BinaryBitmap binarybitmapobj = new BinaryBitmap(new HybridBinarizer(source));
 
    Result resultobj = new MultiFormatReader().decode(binarybitmapobj);
 
    System.out.println("Data Stored In our QR Code" +"  " + resultobj.getText());
    }
    
}

In the above code javaapplication29 is the name of our package. We imported all the packages required to run the code. Package com.google.zxing belongs to java 3.3.0 jar but com.google.zxing.client.j2se.BufferedImageLuminanceSource and com.google.zxing.common.HybridBinarizer belongs to JavaSE 3.3.3.

NewClass is the name of our class. In the main method of our class, we used several methods.

Methods used in our code

BufferedImage is a subclass of the Image class. We use the BufferedImage class to work with images.

ImageIO:: Belongs to a utility class. It provides us several methods to work with images. We used the read method to read our QR Code. We passed the instance of our QR Code image as a parameter of the read method of utility class.

BinaryBitmap is used to read one line of the luminance data from the QR Code and convert it to one-bit data then rotate it with 90 degrees and repeating the same process again and further rotating the image by 45 degrees and so on. Luminance data is the data generated due to colors from which we pass light to read the data. Data stored depends on the intensity of the color used.

MultiFormatReader is the main class used in the zxing file. As we use the MultiFormatReader in most of the cases to decode the coded data.

Further, we used the getText method with the object of the Result class. We use the getText method to get the data stored in the Result instead of the message-id of the data.

System.out.println() to print the required data as the output of our code.

Image of QR Code decoded:

qr code java

Output:

QR Code to Text converter in Java

Also read:

Leave a Reply

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