Java — Read QR Code using zxing | Code Factory

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
package com.example.java.programming;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.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
/**
* @author code.factory
*
*/
public class QRCodeReader {
private static final String IMAGE_PATH = "QR.png"; public static void main(String... args) {
try {
File file = new File(IMAGE_PATH);
String str = readQRCode(file);
if (str == null) {
System.out.println("No QR Code found in the Image.");
} else {
System.out.println("Decoded = " + str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String readQRCode(File file) throws IOException {
BufferedImage bufferedImage = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
Result result = new MultiFormatReader().decode(bitmap);
return result.getText();
} catch (NotFoundException e) {
System.out.println("There is no QR code in the Image.");
return null;
}
}
}
Decoded = Code Factory

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ClosedSea NFT Marketplace Testnet V2 Now Live

What You Can Do With Snowflake

How to Setup a Custom Task to Capture Client ID, Session ID, and Hit ID Tracking using Google Tag…

How To Create A Popular Mobile App Like Calm in 2020

Keycloak — part 2

Facebook libra on Windows 10

A Conversation with Software Engineer Adrienne Domingus

Avoid integration testing against shared environments

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Concurrency- Chapter1- Atomic & Collections

Design Pattern in Java — Builder Pattern

Comparison of Java 8 vs Java 17, Should You Upgrade?

SOLID Principles in Java