2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common;
19 import com.google.zxing.BinaryBitmap;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
25 import org.junit.Test;
27 import javax.imageio.ImageIO;
28 import java.awt.image.BufferedImage;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
35 * This abstract class looks for negative results, i.e. it only allows a certain number of false
36 * positives in images which should not decode. This helps ensure that we are not too lenient.
38 * @author dswitkin@google.com (Daniel Switkin)
40 public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
42 private static class TestResult {
43 private final int falsePositivesAllowed;
44 private final float rotation;
46 TestResult(int falsePositivesAllowed, float rotation) {
47 this.falsePositivesAllowed = falsePositivesAllowed;
48 this.rotation = rotation;
51 public int getFalsePositivesAllowed() {
52 return falsePositivesAllowed;
55 public float getRotation() {
60 private final List<TestResult> testResults;
62 // Use the multiformat reader to evaluate all decoders in the system.
63 protected AbstractNegativeBlackBoxTestCase(String testBasePathSuffix) {
64 super(testBasePathSuffix, new MultiFormatReader(), null);
65 testResults = new ArrayList<TestResult>();
68 protected void addTest(int falsePositivesAllowed, float rotation) {
69 testResults.add(new TestResult(falsePositivesAllowed, rotation));
74 public void testBlackBox() throws IOException {
75 assertFalse(testResults.isEmpty());
77 File[] imageFiles = getImageFiles();
78 int[] falsePositives = new int[testResults.size()];
79 for (File testImage : imageFiles) {
80 System.out.println("Starting " + testImage.getAbsolutePath());
82 BufferedImage image = ImageIO.read(testImage);
84 throw new IOException("Could not read image: " + testImage);
86 for (int x = 0; x < testResults.size(); x++) {
87 if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
93 for (int x = 0; x < testResults.size(); x++) {
94 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " +
95 falsePositives[x] + " of " + imageFiles.length + " images were false positives (" +
96 testResults.get(x).getFalsePositivesAllowed() + " allowed)");
97 assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: " +
98 "Too many false positives found",
99 falsePositives[x] <= testResults.get(x).getFalsePositivesAllowed());
104 * Make sure ZXing does NOT find a barcode in the image.
106 * @param image The image to test
107 * @param rotationInDegrees The amount of rotation to apply
108 * @return true if nothing found, false if a non-existant barcode was detected
110 private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
111 BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
112 LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
113 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
116 result = getReader().decode(bitmap);
117 System.out.println("Found false positive: '" + result.getText() + "' with format '" +
118 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
120 } catch (ReaderException re) {
123 // Try "try harder" getMode
125 result = getReader().decode(bitmap, TRY_HARDER_HINT);
126 System.out.println("Try harder found false positive: '" + result.getText() +
127 "' with format '" + result.getBarcodeFormat() + "' (rotation: " +
128 rotationInDegrees + ')');
130 } catch (ReaderException re) {