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