X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javase%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2se%2FCommandLineRunner.java;h=78f7626577b894049c49536a26cae8a9edcd27fa;hb=c4c6e602140cee090e8ee0ed8299d15d8a8ebde6;hp=cac8e4ff797e2bb50f53855e724cc63efb1e2e7d;hpb=59a995beefc05ba1a676bd07997f24bdfe87d50b;p=zxing.git
diff --git a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java
index cac8e4ff..78f76265 100644
--- a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java
+++ b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java
@@ -21,13 +21,19 @@ import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
+import com.google.zxing.client.result.ParsedResult;
+import com.google.zxing.client.result.ResultParser;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
-import java.io.IOException;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
import java.net.URI;
+import java.nio.charset.Charset;
import java.util.Hashtable;
/**
@@ -36,7 +42,8 @@ import java.util.Hashtable;
* request that hint. The raw text of each barcode is printed, and when running against directories,
* summary statistics are also displayed.
*
- * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
+ * @author Sean Owen
+ * @author dswitkin@google.com (Daniel Switkin)
*/
public final class CommandLineRunner {
@@ -45,10 +52,13 @@ public final class CommandLineRunner {
public static void main(String[] args) throws Exception {
Hashtable hints = null;
+ boolean dumpResults = false;
for (String arg : args) {
if ("--try_harder".equals(arg)) {
hints = new Hashtable(3);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
+ } else if ("--dump_results".equals(arg)) {
+ dumpResults = true;
} else if (arg.startsWith("--")) {
System.out.println("Unknown command line option " + arg);
return;
@@ -56,13 +66,14 @@ public final class CommandLineRunner {
}
for (String arg : args) {
if (!arg.startsWith("--")) {
- decodeOneArgument(arg, hints);
+ decodeOneArgument(arg, hints, dumpResults);
}
}
}
- private static void decodeOneArgument(String argument, Hashtable hints)
- throws Exception {
+ private static void decodeOneArgument(String argument, Hashtable hints,
+ boolean dumpResults) throws Exception {
+
File inputFile = new File(argument);
if (inputFile.exists()) {
if (inputFile.isDirectory()) {
@@ -74,22 +85,48 @@ public final class CommandLineRunner {
if (filename.startsWith(".") || filename.endsWith(".txt")) {
continue;
}
- if (decode(input.toURI(), hints)) {
+ Result result = decode(input.toURI(), hints);
+ if (result != null) {
successful++;
+ if (dumpResults) {
+ dumpResult(input, result);
+ }
}
total++;
}
System.out.println("\nDecoded " + successful + " files out of " + total +
" successfully (" + (successful * 100 / total) + "%)\n");
} else {
- decode(inputFile.toURI(), hints);
+ Result result = decode(inputFile.toURI(), hints);
+ if (dumpResults) {
+ dumpResult(inputFile, result);
+ }
}
} else {
decode(new URI(argument), hints);
}
}
- private static boolean decode(URI uri, Hashtable hints) throws IOException {
+ private static void dumpResult(File input, Result result) throws IOException {
+ String name = input.getAbsolutePath();
+ int pos = name.lastIndexOf('.');
+ if (pos > 0) {
+ name = name.substring(0, pos);
+ }
+ File dump = new File(name + ".txt");
+ writeStringToFile(result.getText(), dump);
+ }
+
+ private static void writeStringToFile(String value, File file) throws IOException {
+ Writer out = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF8"));
+ try {
+ out.write(value);
+ } finally {
+ out.close();
+ }
+ }
+
+ private static Result decode(URI uri, Hashtable hints) throws IOException {
BufferedImage image;
try {
image = ImageIO.read(uri.toURL());
@@ -98,17 +135,19 @@ public final class CommandLineRunner {
}
if (image == null) {
System.err.println(uri.toString() + ": Could not load image");
- return false;
+ return null;
}
try {
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
Result result = new MultiFormatReader().decode(source, hints);
- System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" +
- result.getText());
- return true;
+ ParsedResult parsedResult = ResultParser.parseResult(result);
+ System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() +
+ ", type: " + parsedResult.getType() + "):\nRaw result:\n" + result.getText() +
+ "\nParsed result:\n" + parsedResult.getDisplayResult());
+ return result;
} catch (ReaderException e) {
System.out.println(uri.toString() + ": No barcode found");
- return false;
+ return null;
}
}