Finished work on the local binarizer and renamed it to HybridBinarizer. It uses the...
[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 java.awt.image.BufferedImage;
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.imageio.ImageIO;
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   public void testBlackBox() throws IOException {
74     assertFalse(testResults.isEmpty());
75
76     File[] imageFiles = getImageFiles();
77     int[] falsePositives = new int[testResults.size()];
78     for (File testImage : imageFiles) {
79       System.out.println("Starting " + testImage.getAbsolutePath());
80
81       BufferedImage image = ImageIO.read(testImage);
82       if (image == null) {
83         throw new IOException("Could not read image: " + testImage);
84       }
85       for (int x = 0; x < testResults.size(); x++) {
86         if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
87           falsePositives[x]++;
88         }
89       }
90     }
91
92     for (int x = 0; x < testResults.size(); x++) {
93       System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " +
94           falsePositives[x] + " of " + imageFiles.length + " images were false positives (" +
95           testResults.get(x).getFalsePositivesAllowed() + " allowed)");
96       assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: " +
97           "Too many false positives found",
98           falsePositives[x] <= testResults.get(x).getFalsePositivesAllowed());
99     }
100   }
101
102   /**
103    * Make sure ZXing does NOT find a barcode in the image.
104    *
105    * @param image The image to test
106    * @param rotationInDegrees The amount of rotation to apply
107    * @return true if nothing found, false if a non-existant barcode was detected
108    */
109   private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
110     BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
111     LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
112     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
113     Result result;
114     try {
115       result = getReader().decode(bitmap);
116       System.out.println("Found false positive: '" + result.getText() + "' with format '" +
117           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
118       return false;
119     } catch (ReaderException re) {
120     }
121
122     // Try "try harder" getMode
123     try {
124       result = getReader().decode(bitmap, TRY_HARDER_HINT);
125       System.out.println("Try harder found false positive: '" + result.getText() +
126           "' with format '" + result.getBarcodeFormat() + "' (rotation: " +
127           rotationInDegrees + ')');
128       return false;
129     } catch (ReaderException re) {
130     }
131     return true;
132   }
133
134 }