- Added support for rotation in our blackbox test framework, and refactored the ways...
authordswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 11 Jun 2008 00:05:51 +0000 (00:05 +0000)
committerdswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 11 Jun 2008 00:05:51 +0000 (00:05 +0000)
- Turned on 0 and 180 degree rotation for all 1D formats.
- Turned on 0, 90, 180, and 270 degree rotation for QR.
- Changed the 1D code to re-enable upside down scanning, with a dramatic improvement in barcodes found and fewer false positives.

git-svn-id: http://zxing.googlecode.com/svn/trunk@411 59b500cc-1b3d-0410-9834-0bbf25fbcc57

13 files changed:
core/src/com/google/zxing/oned/AbstractOneDReader.java
core/test/src/com/google/zxing/common/AbstractBlackBoxTestCase.java
core/test/src/com/google/zxing/datamatrix/DataMatrixBlackBox1TestCase.java
core/test/src/com/google/zxing/oned/Code128BlackBox1TestCase.java
core/test/src/com/google/zxing/oned/Code39BlackBox1TestCase.java
core/test/src/com/google/zxing/oned/Code39ExtendedBlackBox2TestCase.java
core/test/src/com/google/zxing/oned/EAN13BlackBox1TestCase.java
core/test/src/com/google/zxing/oned/EAN13BlackBox2TestCase.java
core/test/src/com/google/zxing/oned/EAN8BlackBox1TestCase.java
core/test/src/com/google/zxing/oned/UPCABlackBox1TestCase.java
core/test/src/com/google/zxing/oned/UPCEBlackBox1TestCase.java
core/test/src/com/google/zxing/qrcode/QRCodeBlackBox1TestCase.java
core/test/src/com/google/zxing/qrcode/QRCodeBlackBox2TestCase.java

index d721950..fa1cbaf 100644 (file)
@@ -104,14 +104,11 @@ public abstract class AbstractOneDReader implements OneDReader {
       }
       image.getBlackRow(rowNumber, row, 0, width);
 
-      // We may try twice for each row, if "trying harder":
+      // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
+      // handle decoding upside down barcodes.
       for (int attempt = 0; attempt < 2; attempt++) {
         if (attempt == 1) { // trying again?
-          if (tryHarder) { // only if "trying harder"
-            row.reverse(); // reverse the row and continue
-          } else {
-            break;
-          }
+          row.reverse(); // reverse the row and continue
         }
         try {
           // Look for a barcode
index 6695c4b..1ba5c84 100644 (file)
@@ -26,6 +26,8 @@ import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
 import junit.framework.TestCase;
 
 import javax.imageio.ImageIO;
+import java.awt.geom.AffineTransform;
+import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileInputStream;
@@ -34,9 +36,11 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.Hashtable;
+import java.util.Vector;
 
 /**
  * @author srowen@google.com (Sean Owen)
+ * @author dswitkin@google.com (Daniel Switkin)
  */
 public abstract class AbstractBlackBoxTestCase extends TestCase {
 
@@ -55,27 +59,46 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     }
   };
 
+  private class TestResult {
+    public int mustPassCount;
+    public float rotation;
+
+    TestResult(int mustPassCount, float rotation) {
+      this.mustPassCount = mustPassCount;
+      this.rotation = rotation;
+    }
+  }
+
   private final File testBase;
   private final Reader barcodeReader;
-  private final int mustPassCount;
   private final BarcodeFormat expectedFormat;
+  private Vector<TestResult> testResults;
 
   protected AbstractBlackBoxTestCase(File testBase,
                                      Reader barcodeReader,
-                                     int mustPassCount,
                                      BarcodeFormat expectedFormat) {
     this.testBase = testBase;
     this.barcodeReader = barcodeReader;
-    this.mustPassCount = mustPassCount;
     this.expectedFormat = expectedFormat;
+    testResults = new Vector<TestResult>();
   }
 
-  public void testBlackBox() throws IOException {
+  /**
+   * Adds a new test for the current directory of images.
+   *
+   * @param mustPassCount The number of images which must decode for the test to pass.
+   * @param rotation The rotation in degrees clockwise to use for this test.
+   */
+  protected void addTest(int mustPassCount, float rotation) {
+    testResults.add(new TestResult(mustPassCount, rotation));
+  }
 
+  public void testBlackBox() throws IOException {
+    assertTrue(testResults.size() > 0);
     assertTrue("Please run from the 'core' directory", testBase.exists());
 
     File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
-    int passedCount = 0;
+    int[] passedCounts = new int[testResults.size()];
     for (File testImage : imageFiles) {
       System.out.println("Starting " + testImage.getAbsolutePath());
 
@@ -86,56 +109,67 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
       } else {
         image = ImageIO.read(testImage);
       }
-      MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
-      Result result;
-      try {
-        result = barcodeReader.decode(source);
-      } catch (ReaderException re) {
-        System.out.println(re);
-        continue;
-      }
-
-      if (expectedFormat != result.getBarcodeFormat()) {
-        System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
-            result.getBarcodeFormat() + '\'');
-        continue;
-      }
 
       String testImageFileName = testImage.getName();
       File expectedTextFile = new File(testBase,
           testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
       String expectedText = readFileAsString(expectedTextFile);
-      String resultText = result.getText();
 
-      boolean passed = expectedText.equals(resultText);
-      if (passed) {
-        passedCount++;
-      } else {
-        System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + '\'');
-        continue;
-      }
-
-      // Try "try harder" mode
-      try {
-        result = barcodeReader.decode(source, TRY_HARDER_HINT);
-      } catch (ReaderException re) {
-        if (passed) {
-          fail("Normal mode succeeded but \"try harder\" failed");
+      for (int x = 0; x < testResults.size(); x++) {
+        if (testOneImage(image, testResults.get(x).rotation, expectedText)) {
+          passedCounts[x]++;
         }
-        continue;
-      }
-      if (expectedFormat != result.getBarcodeFormat()) {
-        System.out.println("Try Harder Format mismatch: expected '" + expectedFormat + "' but got '" +
-            result.getBarcodeFormat() + '\'');
-      } else if (!expectedText.equals(resultText)) {
-        System.out.println("Try Harder Mismatch: expected '" + expectedText + "' but got '" +
-            resultText + '\'');
       }
     }
 
-    System.out.println(passedCount + " of " + imageFiles.length + " images passed (" +
-        mustPassCount + " required)");
-    assertTrue("Too many images failed", passedCount >= mustPassCount);
+    for (int x = 0; x < testResults.size(); x++) {
+      System.out.println("Rotation " + testResults.get(x).rotation + " degrees: " + passedCounts[x] +
+          " of " + imageFiles.length + " images passed (" + testResults.get(x).mustPassCount +
+          " required)");
+      assertTrue("Rotation " + testResults.get(x).rotation + " degrees: Too many images failed",
+          passedCounts[x] >= testResults.get(x).mustPassCount);
+    }
+  }
+
+  private boolean testOneImage(BufferedImage image, float rotationInDegrees, String expectedText) {
+    BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
+    MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
+    Result result;
+    try {
+      result = barcodeReader.decode(source);
+    } catch (ReaderException re) {
+      System.out.println(re);
+      return false;
+    }
+
+    if (expectedFormat != result.getBarcodeFormat()) {
+      System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
+          result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ")");
+      return false;
+    }
+
+    String resultText = result.getText();
+    if (!expectedText.equals(resultText)) {
+      System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
+          "' (rotation: " + rotationInDegrees + ")");
+      return false;
+    }
+
+    // Try "try harder" mode
+    try {
+      result = barcodeReader.decode(source, TRY_HARDER_HINT);
+    } catch (ReaderException re) {
+      fail("Normal mode succeeded but \"try harder\" failed");
+      return false;
+    }
+    if (expectedFormat != result.getBarcodeFormat()) {
+      System.out.println("Try Harder Format mismatch: expected '" + expectedFormat + "' but got '" +
+          result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ")");
+    } else if (!expectedText.equals(resultText)) {
+      System.out.println("Try Harder Mismatch: expected '" + expectedText + "' but got '" +
+          resultText + "' (rotation: " + rotationInDegrees + ")");
+    }
+    return true;
   }
 
   private static String readFileAsString(File file) throws IOException {
@@ -153,4 +187,15 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     return result.toString();
   }
 
+  private static BufferedImage rotateImage(BufferedImage original, float degrees) {
+    if (degrees != 0.0f) {
+      AffineTransform at = new AffineTransform();
+      at.rotate(Math.toRadians(degrees), original.getWidth() / 2, original.getHeight() / 2);
+      AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
+      return op.filter(original, null);
+    } else {
+      return original;
+    }
+  }
+
 }
\ No newline at end of file
index f3e690a..24d66d8 100644 (file)
@@ -29,7 +29,8 @@ public final class DataMatrixBlackBox1TestCase extends AbstractBlackBoxTestCase
 
   public DataMatrixBlackBox1TestCase() {
     // TODO use MultiFormatReader here once Data Matrix decoder is done
-    super(new File("test/data/blackbox/datamatrix-1"), new DataMatrixReader(), 7, BarcodeFormat.DATAMATRIX);
+    super(new File("test/data/blackbox/datamatrix-1"), new DataMatrixReader(), BarcodeFormat.DATAMATRIX);
+    addTest(7, 0.0f);
   }
 
 }
\ No newline at end of file
index c9fec32..e24a1c2 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class Code128BlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public Code128BlackBox1TestCase() {
-    super(new File("test/data/blackbox/code128-1"), new MultiFormatReader(), 5, BarcodeFormat.CODE_128);
+    super(new File("test/data/blackbox/code128-1"), new MultiFormatReader(), BarcodeFormat.CODE_128);
+    addTest(5, 0.0f);
+    addTest(5, 180.0f);
   }
 
 }
\ No newline at end of file
index ee68b51..5106166 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class Code39BlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public Code39BlackBox1TestCase() {
-    super(new File("test/data/blackbox/code39-1"), new MultiFormatReader(), 4, BarcodeFormat.CODE_39);
+    super(new File("test/data/blackbox/code39-1"), new MultiFormatReader(), BarcodeFormat.CODE_39);
+    addTest(4, 0.0f);
+    addTest(4, 180.0f);
   }
 
 }
\ No newline at end of file
index 270524e..e61a95c 100644 (file)
@@ -27,7 +27,9 @@ import java.io.File;
 public final class Code39ExtendedBlackBox2TestCase extends AbstractBlackBoxTestCase {
 
   public Code39ExtendedBlackBox2TestCase() {
-    super(new File("test/data/blackbox/code39-2"), new Code39Reader(false, true), 2, BarcodeFormat.CODE_39);
+    super(new File("test/data/blackbox/code39-2"), new Code39Reader(false, true), BarcodeFormat.CODE_39);
+    addTest(2, 0.0f);
+    addTest(2, 180.0f);
   }
 
 }
\ No newline at end of file
index 29fb9c2..7e28d88 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class EAN13BlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public EAN13BlackBox1TestCase() {
-    super(new File("test/data/blackbox/ean13-1"), new MultiFormatReader(), 25, BarcodeFormat.EAN_13);
+    super(new File("test/data/blackbox/ean13-1"), new MultiFormatReader(), BarcodeFormat.EAN_13);
+    addTest(25, 0.0f);
+    addTest(18, 180.0f);
   }
 
 }
\ No newline at end of file
index 514972d..a2d71ab 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class EAN13BlackBox2TestCase extends AbstractBlackBoxTestCase {
 
   public EAN13BlackBox2TestCase() {
-    super(new File("test/data/blackbox/ean13-2"), new MultiFormatReader(), 1, BarcodeFormat.EAN_13);
+    super(new File("test/data/blackbox/ean13-2"), new MultiFormatReader(), BarcodeFormat.EAN_13);
+    addTest(1, 0.0f);
+    addTest(1, 180.0f);
   }
 
 }
\ No newline at end of file
index 2cd3a1c..6e6b22c 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class EAN8BlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public EAN8BlackBox1TestCase() {
-    super(new File("test/data/blackbox/ean8-1"), new MultiFormatReader(), 8, BarcodeFormat.EAN_8);
+    super(new File("test/data/blackbox/ean8-1"), new MultiFormatReader(), BarcodeFormat.EAN_8);
+    addTest(8, 0.0f);
+    addTest(7, 180.0f);
   }
 
 }
\ No newline at end of file
index bcaf63b..0d3e0d3 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class UPCABlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public UPCABlackBox1TestCase() {
-    super(new File("test/data/blackbox/upca-1"), new MultiFormatReader(), 15, BarcodeFormat.UPC_A);
+    super(new File("test/data/blackbox/upca-1"), new MultiFormatReader(), BarcodeFormat.UPC_A);
+    addTest(15, 0.0f);
+    addTest(16, 180.0f);
   }
 
 }
\ No newline at end of file
index 56c487e..7a59336 100644 (file)
@@ -28,7 +28,9 @@ import java.io.File;
 public final class UPCEBlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public UPCEBlackBox1TestCase() {
-    super(new File("test/data/blackbox/upce-1"), new MultiFormatReader(), 3, BarcodeFormat.UPC_E);
+    super(new File("test/data/blackbox/upce-1"), new MultiFormatReader(), BarcodeFormat.UPC_E);
+    addTest(3, 0.0f);
+    addTest(3, 180.0f);
   }
 
 }
\ No newline at end of file
index a0cdba9..e46746a 100644 (file)
@@ -28,7 +28,11 @@ import java.io.File;
 public final class QRCodeBlackBox1TestCase extends AbstractBlackBoxTestCase {
 
   public QRCodeBlackBox1TestCase() {
-    super(new File("test/data/blackbox/qrcode-1"), new MultiFormatReader(), 16, BarcodeFormat.QR_CODE);
+    super(new File("test/data/blackbox/qrcode-1"), new MultiFormatReader(), BarcodeFormat.QR_CODE);
+    addTest(16, 0.0f);
+    addTest(12, 90.0f);
+    addTest(12, 180.0f);
+    addTest(7, 270.0f);
   }
 
 }
\ No newline at end of file
index 4c2f0dc..a7a5988 100644 (file)
@@ -28,7 +28,11 @@ import java.io.File;
 public final class QRCodeBlackBox2TestCase extends AbstractBlackBoxTestCase {
 
   public QRCodeBlackBox2TestCase() {
-    super(new File("test/data/blackbox/qrcode-2"), new MultiFormatReader(), 10, BarcodeFormat.QR_CODE);
+    super(new File("test/data/blackbox/qrcode-2"), new MultiFormatReader(), BarcodeFormat.QR_CODE);
+    addTest(10, 0.0f);
+    addTest(5, 90.0f);
+    addTest(8, 180.0f);
+    addTest(2, 270.0f);
   }
 
 }
\ No newline at end of file