/*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package com.google.zxing.client.j2se;
import com.google.zxing.DecodeHintType;
+import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
-import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.Result;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
+import java.io.FileNotFoundException;
import java.net.URI;
import java.util.Hashtable;
*/
public final class CommandLineRunner {
- private static Hashtable<DecodeHintType, Object> hints = null;
-
private CommandLineRunner() {
}
public static void main(String[] args) throws Exception {
- for (int x = 0; x < args.length; x++) {
- if (args[x].equals("--try_harder")) {
+ 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 (args[x].startsWith("--")) {
- System.out.println("Unknown command line option " + args[x]);
+ } else if (arg.startsWith("--")) {
+ System.out.println("Unknown command line option " + arg);
return;
}
}
- for (int x = 0; x < args.length; x++) {
- if (!args[x].startsWith("--")) {
- decodeOneArgument(args[x]);
+ for (String arg : args) {
+ if (!arg.startsWith("--")) {
+ decodeOneArgument(arg, hints);
}
}
}
- private static void decodeOneArgument(String argument) throws Exception {
+ private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints)
+ throws Exception {
File inputFile = new File(argument);
if (inputFile.exists()) {
if (inputFile.isDirectory()) {
if (filename.startsWith(".") || filename.endsWith(".txt")) {
continue;
}
- if (decode(input.toURI())) {
+ if (decode(input.toURI(), hints)) {
successful++;
}
total++;
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(argument));
+ decode(new URI(argument), hints);
}
}
- private static boolean decode(URI uri) throws IOException {
- BufferedImage image = ImageIO.read(uri.toURL());
+ private static boolean decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException {
+ BufferedImage image;
+ try {
+ image = ImageIO.read(uri.toURL());
+ } catch (IllegalArgumentException iae) {
+ throw new FileNotFoundException("Resource not found: " + uri);
+ }
if (image == null) {
System.err.println(uri.toString() + ": Could not load image");
return false;
}
try {
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
- String result = new MultiFormatReader().decode(source, hints).getText();
- System.out.println(uri.toString() + ": " + result);
+ Result result = new MultiFormatReader().decode(source, hints);
+ System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" +
+ result.getText());
return true;
} catch (ReaderException e) {
System.out.println(uri.toString() + ": No barcode found");