Read QR Code In Java Using ZXing Library

In this article we will see how to read QR code in java. We are going to use ZXing (Zebra Crossing) library for this. QR stands for Quick Response. Below is an example of QR Code. Open your mobile's camera and put that in front of this image (QR Code) and you will see URL as http://www.infybuzz.com which is basically data this QR Code is containing.


We have already seen How Generate QR Code In Java Using ZXing Library In this post we will decode the QR code in Java using ZXing library. This QR code contains text that is http://www.infybuzz.com

So lets get started by adding ZXing dependencies. Add below dependencies in your pom.xml OR build.gradle file.


		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>core</artifactId>
			<version>3.4.1</version>
		</dependency>

		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.4.1</version>
		</dependency>

After this lets move to code part to read(decode) QR code which is containing text that is http://www.infybuzz.com


import java.awt.image.BufferedImage;
import java.io.FileInputStream;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQRCode {

	public static void main(String[] args) throws Exception {
		
		try {
			String path = "D:\\QR-Code\\infybuzz.jpg";
			
			BufferedImage bf = ImageIO.read(new FileInputStream(path));
			
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
					new BufferedImageLuminanceSource(bf)));
			
			Result result = new MultiFormatReader().decode(bitmap);
			
			System.out.println(result.getText());
			
		} catch(Exception e) {
			
		}
	}

}


0 Comments