Small style stuff
[zxing.git] / core / test / src / com / google / zxing / common / AbstractNegativeBlackBoxTestCase.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.common;
18
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;
26
27 import javax.imageio.ImageIO;
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
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.
37  *
38  * @author dswitkin@google.com (Daniel Switkin)
39  */
40 public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
41
42   private static class TestResult {
43     private final int falsePositivesAllowed;
44     private final float rotation;
45
46     TestResult(int falsePositivesAllowed, float rotation) {
47       this.falsePositivesAllowed = falsePositivesAllowed;
48       this.rotation = rotation;
49     }
50
51     public int getFalsePositivesAllowed() {
52       return falsePositivesAllowed;
53     }
54
55     public float getRotation() {
56       return rotation;
57     }
58   }
59
60   private final List<TestResult> testResults;
61
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>();
66   }
67
68   protected void addTest(int falsePositivesAllowed, float rotation) {
69     testResults.add(new TestResult(falsePositivesAllowed, rotation));
70   }
71
72   @Override
73   @Test
74   public void testBlackBox() throws IOException {
75     assertFalse(testResults.isEmpty());
76
77     File[] imageFiles = getImageFiles();
78     int[] falsePositives = new int[testResults.size()];
79     for (File testImage : imageFiles) {
80       System.out.println("Starting " + testImage.getAbsolutePath());
81
82       BufferedImage image = ImageIO.read(testImage);
83       if (image == null) {
84         throw new IOException("Could not read image: " + testImage);
85       }
86       for (int x = 0; x < testResults.size(); x++) {
87         if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
88           falsePositives[x]++;
89         }
90       }
91     }
92
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());
100     }
101   }
102
103   /**
104    * Make sure ZXing does NOT find a barcode in the image.
105    *
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
109    */
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));
114     Result result;
115     try {
116       result = getReader().decode(bitmap);
117       System.out.println("Found false positive: '" + result.getText() + "' with format '" +
118           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
119       return false;
120     } catch (ReaderException re) {
121     }
122
123     // Try "try harder" getMode
124     try {
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 + ')');
129       return false;
130     } catch (ReaderException re) {
131     }
132     return true;
133   }
134
135 }