Fixed crash and bad parsing of an SMS with a question mark in the subject or message.
[zxing.git] / javase / src / com / google / zxing / client / j2se / CommandLineRunner.java
index 90f32d7..78f7626 100644 (file)
@@ -21,6 +21,8 @@ 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;
@@ -28,7 +30,10 @@ import java.io.File;
 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;
 
 /**
@@ -37,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.</p>
  *
- * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
+ * @author Sean Owen
+ * @author dswitkin@google.com (Daniel Switkin)
  */
 public final class CommandLineRunner {
 
@@ -83,16 +89,7 @@ public final class CommandLineRunner {
           if (result != null) {
             successful++;
             if (dumpResults) {
-              String name = input.getAbsolutePath();
-              int pos = name.lastIndexOf('.');
-              if (pos > 0) {
-                name = name.substring(0, pos);
-              }
-              File dump = new File(name + ".txt");
-              dump.createNewFile();
-              FileOutputStream stream = new FileOutputStream(dump);
-              stream.write(result.getText().getBytes());
-              stream.close();
+              dumpResult(input, result);
             }
           }
           total++;
@@ -100,13 +97,35 @@ public final class CommandLineRunner {
         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 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<DecodeHintType, Object> hints) throws IOException {
     BufferedImage image;
     try {
@@ -121,8 +140,10 @@ public final class CommandLineRunner {
     try {
       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
       Result result = new MultiFormatReader().decode(source, hints);
-      System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" +
-          result.getText());
+      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");