Added the ability to track who is throwing exceptions and how often in the core libra...
[zxing.git] / javase / src / com / google / zxing / client / j2se / CommandLineRunner.java
index fba1e9b..f9ccf7c 100644 (file)
 
 package com.google.zxing.client.j2se;
 
-import com.google.zxing.BlackPointEstimationMethod;
+import com.google.zxing.BinaryBitmap;
 import com.google.zxing.DecodeHintType;
-import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.LuminanceSource;
 import com.google.zxing.MultiFormatReader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
+import com.google.zxing.BarcodeFormat;
 import com.google.zxing.client.result.ParsedResult;
 import com.google.zxing.client.result.ResultParser;
 import com.google.zxing.common.BitArray;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.common.GlobalHistogramBinarizer;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
@@ -38,6 +41,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.util.Hashtable;
+import java.util.Vector;
 
 import javax.imageio.ImageIO;
 
@@ -60,19 +64,18 @@ public final class CommandLineRunner {
       printUsage();
       return;
     }
-    Hashtable<DecodeHintType, Object> hints = null;
+    Hashtable<DecodeHintType, Object> hints = buildHints();
     boolean dumpResults = false;
     boolean dumpBlackPoint = false;
     for (String arg : args) {
       if ("--try_harder".equals(arg)) {
-        hints = new Hashtable<DecodeHintType, Object>(3);
         hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
       } else if ("--dump_results".equals(arg)) {
         dumpResults = true;
       } else if ("--dump_black_point".equals(arg)) {
         dumpBlackPoint = true;
       } else if (arg.startsWith("-")) {
-        System.out.println("Unknown command line option " + arg);
+        System.err.println("Unknown command line option " + arg);
         printUsage();
         return;
       }
@@ -84,12 +87,30 @@ public final class CommandLineRunner {
     }
   }
 
+  // Manually turn on all formats, even those not yet considered production quality.
+  private static Hashtable<DecodeHintType, Object> buildHints() {
+    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
+    Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(8);
+    vector.addElement(BarcodeFormat.UPC_A);
+    vector.addElement(BarcodeFormat.UPC_E);
+    vector.addElement(BarcodeFormat.EAN_13);
+    vector.addElement(BarcodeFormat.EAN_8);
+    vector.addElement(BarcodeFormat.CODE_39);
+    vector.addElement(BarcodeFormat.CODE_128);
+    vector.addElement(BarcodeFormat.ITF);
+    vector.addElement(BarcodeFormat.QR_CODE);
+    vector.addElement(BarcodeFormat.DATAMATRIX);
+    vector.addElement(BarcodeFormat.PDF417);
+    hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
+    return hints;
+  }
+
   private static void printUsage() {
-    System.out.println("Decode barcode images using the ZXing library\n");
-    System.out.println("usage: CommandLineRunner { file | dir | url } [ options ]");
-    System.out.println("  --try_harder: Use the TRY_HARDER hint, default is normal (mobile) mode");
-    System.out.println("  --dump_results: Write the decoded contents to input.txt");
-    System.out.println("  --dump_black_point: Compare black point algorithms as input.mono.png");
+    System.err.println("Decode barcode images using the ZXing library\n");
+    System.err.println("usage: CommandLineRunner { file | dir | url } [ options ]");
+    System.err.println("  --try_harder: Use the TRY_HARDER hint, default is normal (mobile) mode");
+    System.err.println("  --dump_results: Write the decoded contents to input.txt");
+    System.err.println("  --dump_black_point: Compare black point algorithms as input.mono.png");
   }
 
   private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints,
@@ -165,11 +186,12 @@ public final class CommandLineRunner {
       return null;
     }
     try {
-      MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
+      LuminanceSource source = new BufferedImageLuminanceSource(image);
+      BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
       if (dumpBlackPoint) {
-        dumpBlackPoint(uri, image, source);
+        dumpBlackPoint(uri, image, bitmap);
       }
-      Result result = new MultiFormatReader().decode(source, hints);
+      Result result = new MultiFormatReader().decode(bitmap, hints);
       ParsedResult parsedResult = ResultParser.parseResult(result);
       System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() +
           ", type: " + parsedResult.getType() + "):\nRaw result:\n" + result.getText() +
@@ -178,20 +200,25 @@ public final class CommandLineRunner {
     } catch (ReaderException e) {
       System.out.println(uri.toString() + ": No barcode found");
       return null;
+    } finally {
+      // Uncomment these lines when turning on exception tracking in ReaderException.
+      //System.out.println("Threw " + ReaderException.getExceptionCountAndReset() + " exceptions");
+      //System.out.println("Throwers:\n" + ReaderException.getThrowersAndReset());
     }
   }
 
   // Writes out a single PNG which is three times the width of the input image, containing from left
   // to right: the original image, the row sampling monochrome version, and the 2D sampling
   // monochrome version.
-  private static void dumpBlackPoint(URI uri, BufferedImage image, MonochromeBitmapSource source) {
+  // TODO: Update to compare different Binarizer implementations.
+  private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) {
     String inputName = uri.getPath();
     if (inputName.contains(".mono.png")) {
       return;
     }
 
-    int width = source.getWidth();
-    int height = source.getHeight();
+    int width = bitmap.getWidth();
+    int height = bitmap.getHeight();
     int stride = width * 3;
     int[] pixels = new int[stride * height];
 
@@ -201,22 +228,21 @@ public final class CommandLineRunner {
       image.getRGB(0, y, width, 1, argb, 0, width);
       System.arraycopy(argb, 0, pixels, y * stride, width);
     }
-    argb = null;
 
     // Row sampling
     BitArray row = new BitArray(width);
     for (int y = 0; y < height; y++) {
       try {
-        source.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, y);
+        row = bitmap.getBlackRow(y, row);
       } catch (ReaderException e) {
-        // If the row histogram failed, draw a red line and keep going
+        // If fetching the row failed, draw a red line and keep going.
         int offset = y * stride + width;
         for (int x = 0; x < width; x++) {
           pixels[offset + x] = 0xffff0000;
         }
         continue;
       }
-      row = source.getBlackRow(y, row, 0, width);
+
       int offset = y * stride + width;
       for (int x = 0; x < width; x++) {
         if (row.get(x)) {
@@ -229,12 +255,11 @@ public final class CommandLineRunner {
 
     // 2D sampling
     try {
-      source.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);
       for (int y = 0; y < height; y++) {
-        row = source.getBlackRow(y, row, 0, width);
+        BitMatrix matrix = bitmap.getBlackMatrix();
         int offset = y * stride + width * 2;
         for (int x = 0; x < width; x++) {
-          if (row.get(x)) {
+          if (matrix.get(x, y)) {
             pixels[offset + x] = 0xff000000;
           } else {
             pixels[offset + x] = 0xffffffff;
@@ -250,10 +275,10 @@ public final class CommandLineRunner {
 
     // Use the current working directory for URLs
     String resultName = inputName;
-    if (uri.getScheme().equals("http")) {
+    if ("http".equals(uri.getScheme())) {
       int pos = resultName.lastIndexOf('/');
       if (pos > 0) {
-        resultName = "." + resultName.substring(pos);
+        resultName = '.' + resultName.substring(pos);
       }
     }
     int pos = resultName.lastIndexOf('.');
@@ -261,13 +286,22 @@ public final class CommandLineRunner {
       resultName = resultName.substring(0, pos);
     }
     resultName += ".mono.png";
+    OutputStream outStream = null;
     try {
-      OutputStream outStream = new FileOutputStream(resultName);
+      outStream = new FileOutputStream(resultName);
       ImageIO.write(result, "png", outStream);
     } catch (FileNotFoundException e) {
-      System.out.println("Could not create " + resultName);
+      System.err.println("Could not create " + resultName);
     } catch (IOException e) {
-      System.out.println("Could not write to " + resultName);
+      System.err.println("Could not write to " + resultName);
+    } finally {
+      try {
+        if (outStream != null) {
+          outStream.close();
+        }
+      } catch (IOException ioe) {
+        // continue
+      }
     }
   }