Skip to content

Instantly share code, notes, and snippets.

@luisparravicini
Created March 30, 2019 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisparravicini/52d601941f8e80d6b79bc013793e13d7 to your computer and use it in GitHub Desktop.
Save luisparravicini/52d601941f8e80d6b79bc013793e13d7 to your computer and use it in GitHub Desktop.
//
// Tries to decode all the files on a certain directory and outputs the result:
// System.out for the files with a qr code
// System.err for the files without a qr code
//
// based on code from https://stackoverflow.com/questions/36210537/find-qr-code-in-image-and-decode-it-using-zxing
//
// needs the ZXing jars from:
// https://oss.sonatype.org/content/repositories/snapshots/com/google/zxing/core/3.3.4-SNAPSHOT/core-3.3.4-20181025.133109-2.jar
// https://oss.sonatype.org/content/repositories/snapshots/com/google/zxing/javase/3.3.4-SNAPSHOT/javase-3.3.4-20181025.133127-2.jar
//
//
//package com.attendance.mark;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRDecoder {
public static void main(String[] args) throws WriterException, IOException,
NotFoundException {
String charset = "UTF-8"; // or "ISO-8859-1"
Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
if (args.length == 0) {
System.err.println("Need a directory path as argument");
System.exit(1);
}
String dirArg = args[0];
File dir = new File(dirArg);
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
String fname = child.getName();
if (fname.startsWith("."))
continue;
try {
String filePath = child.getAbsolutePath();
System.out.println(fname + "\t"
+ readQRCode(filePath, charset, hintMap));
} catch (NotFoundException e) {
System.err.println(fname + "\tNo QR found");
}
}
} else {
System.err.println(dirArg + " is not a directory");
}
}
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
return qrCodeResult.getText();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment