Created a library of images which do not contain barcodes, or contain barcodes we...
authordswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 17 Jun 2008 18:35:16 +0000 (18:35 +0000)
committerdswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 17 Jun 2008 18:35:16 +0000 (18:35 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@436 59b500cc-1b3d-0410-9834-0bbf25fbcc57

28 files changed:
core/test/data/blackbox/falsepositives/01.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/02.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/03.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/04.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/05.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/06.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/07.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/08.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/09.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/10.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/11.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/12.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/13.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/14.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/15.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/16.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/17.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/18.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/19.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/20.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/21.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/22.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/23.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/24.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/25.jpg [new file with mode: 0644]
core/test/data/blackbox/falsepositives/26.jpg [new file with mode: 0644]
core/test/src/com/google/zxing/common/AbstractBlackBoxTestCase.java
core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java [new file with mode: 0644]

diff --git a/core/test/data/blackbox/falsepositives/01.jpg b/core/test/data/blackbox/falsepositives/01.jpg
new file mode 100644 (file)
index 0000000..0ea27f8
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/01.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/02.jpg b/core/test/data/blackbox/falsepositives/02.jpg
new file mode 100644 (file)
index 0000000..0b1a578
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/02.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/03.jpg b/core/test/data/blackbox/falsepositives/03.jpg
new file mode 100644 (file)
index 0000000..9db19d8
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/03.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/04.jpg b/core/test/data/blackbox/falsepositives/04.jpg
new file mode 100644 (file)
index 0000000..d25c170
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/04.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/05.jpg b/core/test/data/blackbox/falsepositives/05.jpg
new file mode 100644 (file)
index 0000000..d125b46
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/05.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/06.jpg b/core/test/data/blackbox/falsepositives/06.jpg
new file mode 100644 (file)
index 0000000..65cc1e2
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/06.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/07.jpg b/core/test/data/blackbox/falsepositives/07.jpg
new file mode 100644 (file)
index 0000000..770c9d4
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/07.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/08.jpg b/core/test/data/blackbox/falsepositives/08.jpg
new file mode 100644 (file)
index 0000000..3146ff7
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/08.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/09.jpg b/core/test/data/blackbox/falsepositives/09.jpg
new file mode 100644 (file)
index 0000000..a6d79bd
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/09.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/10.jpg b/core/test/data/blackbox/falsepositives/10.jpg
new file mode 100644 (file)
index 0000000..752f126
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/10.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/11.jpg b/core/test/data/blackbox/falsepositives/11.jpg
new file mode 100644 (file)
index 0000000..8c629d3
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/11.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/12.jpg b/core/test/data/blackbox/falsepositives/12.jpg
new file mode 100644 (file)
index 0000000..a45fa0e
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/12.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/13.jpg b/core/test/data/blackbox/falsepositives/13.jpg
new file mode 100644 (file)
index 0000000..32cd980
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/13.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/14.jpg b/core/test/data/blackbox/falsepositives/14.jpg
new file mode 100644 (file)
index 0000000..fe84b00
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/14.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/15.jpg b/core/test/data/blackbox/falsepositives/15.jpg
new file mode 100644 (file)
index 0000000..ef7ee03
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/15.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/16.jpg b/core/test/data/blackbox/falsepositives/16.jpg
new file mode 100644 (file)
index 0000000..74e338a
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/16.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/17.jpg b/core/test/data/blackbox/falsepositives/17.jpg
new file mode 100644 (file)
index 0000000..abf1775
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/17.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/18.jpg b/core/test/data/blackbox/falsepositives/18.jpg
new file mode 100644 (file)
index 0000000..93a36da
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/18.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/19.jpg b/core/test/data/blackbox/falsepositives/19.jpg
new file mode 100644 (file)
index 0000000..d1cdccb
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/19.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/20.jpg b/core/test/data/blackbox/falsepositives/20.jpg
new file mode 100644 (file)
index 0000000..2ae3f94
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/20.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/21.jpg b/core/test/data/blackbox/falsepositives/21.jpg
new file mode 100644 (file)
index 0000000..687be98
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/21.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/22.jpg b/core/test/data/blackbox/falsepositives/22.jpg
new file mode 100644 (file)
index 0000000..9cbd514
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/22.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/23.jpg b/core/test/data/blackbox/falsepositives/23.jpg
new file mode 100644 (file)
index 0000000..1d121c5
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/23.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/24.jpg b/core/test/data/blackbox/falsepositives/24.jpg
new file mode 100644 (file)
index 0000000..69e4b47
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/24.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/25.jpg b/core/test/data/blackbox/falsepositives/25.jpg
new file mode 100644 (file)
index 0000000..57fc13d
Binary files /dev/null and b/core/test/data/blackbox/falsepositives/25.jpg differ
diff --git a/core/test/data/blackbox/falsepositives/26.jpg b/core/test/data/blackbox/falsepositives/26.jpg
new file mode 100644 (file)
index 0000000..e69de29
index 396f5ed..1e12976 100644 (file)
@@ -44,7 +44,7 @@ import java.util.Vector;
  */
 public abstract class AbstractBlackBoxTestCase extends TestCase {
 
-  private static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
+  protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
   static {
     TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
     TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
@@ -97,11 +97,19 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     testResults.add(new TestResult(mustPassCount, rotation));
   }
 
+  protected File[] getImageFiles() {
+    assertTrue("Please run from the 'core' directory", testBase.exists());
+    return testBase.listFiles(IMAGE_NAME_FILTER);
+  }
+
+  protected Reader getReader() {
+    return barcodeReader;
+  }
+
   public void testBlackBox() throws IOException {
     assertFalse(testResults.isEmpty());
-    assertTrue("Please run from the 'core' directory", testBase.exists());
 
-    File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
+    File[] imageFiles = getImageFiles();
     int[] passedCounts = new int[testResults.size()];
     for (File testImage : imageFiles) {
       System.out.println("Starting " + testImage.getAbsolutePath());
@@ -185,7 +193,7 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     return result.toString();
   }
 
-  private static BufferedImage rotateImage(BufferedImage original, float degrees) {
+  protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
     if (degrees == 0.0f) {
       return original;
     } else {
diff --git a/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java b/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java
new file mode 100644 (file)
index 0000000..a623d11
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common;
+
+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.j2se.BufferedImageMonochromeBitmapSource;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * This test ensures that random, noisy, or unsupported barcode images do not decode.
+ *
+ * @author dswitkin@google.com (Daniel Switkin)
+ */
+public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCase {
+
+  // This number should be reduced as we get better at rejecting false positives.
+  private static final int FALSE_POSITIVES_ALLOWED = 44;
+
+  // Use the multiformat reader to evaluate all decoders in the system.
+  public FalsePositivesBlackBoxTestCase() {
+    super(new File("test/data/blackbox/falsepositives"), new MultiFormatReader(), null);
+  }
+
+  public void testBlackBox() throws IOException {
+    File[] imageFiles = getImageFiles();
+    int falsePositives = 0;
+    for (File testImage : imageFiles) {
+      System.out.println("Starting " + testImage.getAbsolutePath());
+
+      // Try all four rotations, since many of the test images don't have a notion of up, and we
+      // want to be as robust as possible.
+      BufferedImage image = ImageIO.read(testImage);
+      for (int x = 0; x < 4; x++) {
+        if (!checkForFalsePositives(image, x * 90.0f)) {
+          falsePositives++;
+        }
+      }
+    }
+
+    System.out.println("Found " + falsePositives + " false positives (" + FALSE_POSITIVES_ALLOWED +
+        " max)");
+    assertTrue("Too many false positives found", falsePositives <= FALSE_POSITIVES_ALLOWED);
+  }
+
+  /**
+   * Make sure ZXing does NOT find a barcode in the image.
+   *
+   * @param image The image to test
+   * @param rotationInDegrees The amount of rotation to apply
+   * @return true if nothing found, false if a non-existant barcode was detected
+   */
+  private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
+    BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
+    MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
+    Result result;
+    try {
+      result = getReader().decode(source);
+      System.out.println("Found false positive: '" + result.getText() + "' with format '" +
+          result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
+      return false;
+    } catch (ReaderException re) {
+    }
+
+    // Try "try harder" mode
+    try {
+      result = getReader().decode(source, TRY_HARDER_HINT);
+      System.out.println("Try harder found false positive: '" + result.getText() + "' with format '" +
+          result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
+      return false;
+    } catch (ReaderException re) {
+    }
+    return true;
+  }
+
+}