Read Text from QR Code in Java

In this example, I am attaching Simple Program about, How to Read Text from QR Code in Java. The program were tested and output has been shared in the post.

Project Structure

Read Text from QR Code in Java

QR Code

Read Text from QR Code in Java

QRCodeExample.java

package com.dineshkrish.utilities;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
/**
* 
* @author Dinesh Krishnan
*
*/
public class QRCodeExample {
public static void main(String[] args) {
// You can change Image Path accordingly.
String imagePath = "output/dineshkrish.png";
// Defining File Object using Path
File file = new File(imagePath);
try {
// Defining BufferedImage Object using Image File
BufferedImage bufferedImage = ImageIO.read(file);
// Defining BufferedImageLuminanceSource using BufferedImage Object
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
// Defining HybridBinarizer using BufferedImageLuminanceSource Object
HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
// Defining BinaryBitmap using HybridBinarizer
BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
// Defining QRCodeReader Object
QRCodeReader qrCodeReader = new QRCodeReader();
// Decoding Text from QR Code Image
Result result = qrCodeReader.decode(binaryBitmap);
// Getting the Text from Result Object
String decodedText = result.getText();
// Printing the Text
System.out.println("The decoded String value is : "+decodedText);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (ChecksumException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (FormatException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

Output

The decoded String value is : https://idineshkrishnan.com

Download Source Code

1. Download Here

Tags:

No responses yet

Leave a Reply

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