import java.util.Hashtable;
/**
- * <p>Simply attempts to decode the barcode in the image indicated by the single argument
- * to this program, which may be file or a URI. The raw text is printed.</p>
+ * <p>This simple command line utility decodes files, directories of files, or URIs which are passed
+ * as arguments. By default it uses the normal decoding algorithms, but you can pass --try_harder to
+ * request that hint. The raw text of each barcode is printed, and when running against directories,
+ * summary statistics are also displayed.</p>
*
* @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
*/
}
public static void main(String[] args) throws Exception {
- File inputFile = new File(args[0]);
+ Hashtable<DecodeHintType, Object> hints = null;
+ for (String arg : args) {
+ if ("--try_harder".equals(arg)) {
+ hints = new Hashtable<DecodeHintType, Object>(3);
+ hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
+ } else if (arg.startsWith("--")) {
+ System.out.println("Unknown command line option " + arg);
+ return;
+ }
+ }
+ for (String arg : args) {
+ if (!arg.startsWith("--")) {
+ decodeOneArgument(arg, hints);
+ }
+ }
+ }
+
+ private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints) throws Exception {
+ File inputFile = new File(argument);
if (inputFile.exists()) {
if (inputFile.isDirectory()) {
int successful = 0;
+ int total = 0;
for (File input : inputFile.listFiles()) {
- if (decode(input.toURI())) {
+ String filename = input.getName().toLowerCase();
+ // Skip hidden files and text files (the latter is found in the blackbox tests).
+ if (filename.startsWith(".") || filename.endsWith(".txt")) {
+ continue;
+ }
+ if (decode(input.toURI(), hints)) {
successful++;
}
+ total++;
}
- System.out.println("Decoded " + successful + " files successfully");
+ System.out.println("\nDecoded " + successful + " files out of " + total +
+ " successfully (" + (successful * 100 / total) + "%)\n");
} else {
- decode(inputFile.toURI());
+ decode(inputFile.toURI(), hints);
}
} else {
- decode(new URI(args[0]));
+ decode(new URI(argument), hints);
}
}
- private static boolean decode(URI uri) throws IOException {
+ private static boolean decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException {
BufferedImage image = ImageIO.read(uri.toURL());
if (image == null) {
System.err.println(uri.toString() + ": Could not load image");
return false;
}
try {
- Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
- hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
String result = new MultiFormatReader().decode(source, hints).getText();
System.out.println(uri.toString() + ": " + result);
}
}
- /*
- public BufferedImage toBufferedImage() {
- BufferedImage image = new BufferedImage(dimension, dimension, BufferedImage.TYPE_BYTE_BINARY);
- for (int j = 0; j < dimension; j++) {
- for (int i = 0; i < dimension; i++) {
- image.setRGB(j, i, get(i, j) ? 0x00000000 : 0x00FFFFFF);
- }
- }
- return image;
- }
- */
-
}