74fdda814520adfa589f865cfaa40187b84cf4ae
[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
26 import javax.imageio.ImageIO;
27 import java.awt.image.BufferedImage;
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 /**
34  * This abstract class looks for negative results, i.e. it only allows a certain number of false
35  * positives in images which should not decode. This helps ensure that we are not too lenient.
36  *
37  * @author dswitkin@google.com (Daniel Switkin)
38  */
39 public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
40
41   private static class TestResult {
42     private final int falsePositivesAllowed;
43     private final float rotation;
44
45     TestResult(int falsePositivesAllowed, float rotation) {
46       this.falsePositivesAllowed = falsePositivesAllowed;
47       this.rotation = rotation;
48     }
49
50     public int getFalsePositivesAllowed() {
51       return falsePositivesAllowed;
52     }
53
54     public float getRotation() {
55       return rotation;
56     }
57   }
58
59   private final List<TestResult> testResults;
60
61   // Use the multiformat reader to evaluate all decoders in the system.
62   protected AbstractNegativeBlackBoxTestCase(String testBasePathSuffix) {
63     super(testBasePathSuffix, new MultiFormatReader(), null);
64     testResults = new ArrayList<TestResult>();
65   }
66
67   protected void addTest(int falsePositivesAllowed, float rotation) {
68     testResults.add(new TestResult(falsePositivesAllowed, rotation));
69   }
70
71   @Override
72   public void testBlackBox() throws IOException {
73     assertFalse(testResults.isEmpty());
74
75     File[] imageFiles = getImageFiles();
76     int[] falsePositives = new int[testResults.size()];
77     for (File testImage : imageFiles) {
78       System.out.println("Starting " + testImage.getAbsolutePath());
79
80       BufferedImage image = ImageIO.read(testImage);
81       if (image == null) {
82         throw new IOException("Could not read image: " + testImage);
83       }
84       for (int x = 0; x < testResults.size(); x++) {
85         if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
86           falsePositives[x]++;
87         }
88       }
89     }
90
91     for (int x = 0; x < testResults.size(); x++) {
92       System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " +
93           falsePositives[x] + " of " + imageFiles.length + " images were false positives (" +
94           testResults.get(x).getFalsePositivesAllowed() + " allowed)");
95       assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: " +
96           "Too many false positives found",
97           falsePositives[x] <= testResults.get(x).getFalsePositivesAllowed());
98     }
99   }
100
101   /**
102    * Make sure ZXing does NOT find a barcode in the image.
103    *
104    * @param image The image to test
105    * @param rotationInDegrees The amount of rotation to apply
106    * @return true if nothing found, false if a non-existant barcode was detected
107    */
108   private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
109     BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
110     LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
111     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
112     Result result;
113     try {
114       result = getReader().decode(bitmap);
115       System.out.println("Found false positive: '" + result.getText() + "' with format '" +
116           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
117       return false;
118     } catch (ReaderException re) {
119     }
120
121     // Try "try harder" getMode
122     try {
123       result = getReader().decode(bitmap, TRY_HARDER_HINT);
124       System.out.println("Try harder found false positive: '" + result.getText() +
125           "' with format '" + result.getBarcodeFormat() + "' (rotation: " +
126           rotationInDegrees + ')');
127       return false;
128     } catch (ReaderException re) {
129     }
130     return true;
131   }
132
133 }