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.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.DecodeHintType;
22 import com.google.zxing.LuminanceSource;
23 import com.google.zxing.Reader;
24 import com.google.zxing.ReaderException;
25 import com.google.zxing.Result;
26 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
28 import junit.framework.TestCase;
30 import java.awt.geom.AffineTransform;
31 import java.awt.image.AffineTransformOp;
32 import java.awt.image.BufferedImage;
33 import java.awt.image.BufferedImageOp;
35 import java.io.FileInputStream;
36 import java.io.FilenameFilter;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.nio.charset.Charset;
40 import java.util.ArrayList;
41 import java.util.Hashtable;
42 import java.util.List;
44 import javax.imageio.ImageIO;
48 * @author dswitkin@google.com (Daniel Switkin)
50 public abstract class AbstractBlackBoxTestCase extends TestCase {
52 protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
54 TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
55 TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
58 private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
59 public boolean accept(File dir, String name) {
60 String lowerCase = name.toLowerCase();
61 return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
62 lowerCase.endsWith(".gif") || lowerCase.endsWith(".png");
66 public static class SummaryResults {
67 private int totalFound;
68 private int totalMustPass;
69 private int totalTests;
71 public SummaryResults() {
77 public SummaryResults(int found, int mustPass, int total) {
79 totalMustPass = mustPass;
83 public void add(SummaryResults other) {
84 totalFound += other.totalFound;
85 totalMustPass += other.totalMustPass;
86 totalTests += other.totalTests;
89 public String toString() {
90 return "\nSUMMARY RESULTS:\n Decoded " + totalFound + " images out of " + totalTests +
91 " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)";
95 private static class TestResult {
96 private final int mustPassCount;
97 private final int tryHarderCount;
98 private final float rotation;
100 TestResult(int mustPassCount, int tryHarderCount, float rotation) {
101 this.mustPassCount = mustPassCount;
102 this.tryHarderCount = tryHarderCount;
103 this.rotation = rotation;
105 public int getMustPassCount() {
106 return mustPassCount;
108 public int getTryHarderCount() {
109 return tryHarderCount;
111 public float getRotation() {
116 private final File testBase;
117 private final Reader barcodeReader;
118 private final BarcodeFormat expectedFormat;
119 private final List<TestResult> testResults;
121 protected AbstractBlackBoxTestCase(String testBasePathSuffix,
122 Reader barcodeReader,
123 BarcodeFormat expectedFormat) {
124 // A little workaround to prevent aggravation in my IDE
125 File testBase = new File(testBasePathSuffix);
126 if (!testBase.exists()) {
127 // try starting with 'core' since the test base is often given as the project root
128 testBase = new File("core/" + testBasePathSuffix);
130 this.testBase = testBase;
131 this.barcodeReader = barcodeReader;
132 this.expectedFormat = expectedFormat;
133 testResults = new ArrayList<TestResult>();
137 * Adds a new test for the current directory of images.
139 * @param mustPassCount The number of images which must decode for the test to pass.
140 * @param tryHarderCount The number of images which must pass using the try harder flag.
141 * @param rotation The rotation in degrees clockwise to use for this test.
143 protected void addTest(int mustPassCount, int tryHarderCount, float rotation) {
144 testResults.add(new TestResult(mustPassCount, tryHarderCount, rotation));
147 protected File[] getImageFiles() {
148 assertTrue("Please run from the 'core' directory", testBase.exists());
149 return testBase.listFiles(IMAGE_NAME_FILTER);
152 protected Reader getReader() {
153 return barcodeReader;
156 protected Hashtable<DecodeHintType, Object> getHints() {
160 // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does
161 // not return SummaryResults.
162 public void testBlackBox() throws IOException {
163 testBlackBoxCountingResults(true);
166 public SummaryResults testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
167 assertFalse(testResults.isEmpty());
169 File[] imageFiles = getImageFiles();
170 int testCount = testResults.size();
171 int[] passedCounts = new int[testCount];
172 int[] tryHarderCounts = new int[testCount];
173 for (File testImage : imageFiles) {
174 System.out.println("Starting " + testImage.getAbsolutePath());
176 BufferedImage image = ImageIO.read(testImage);
178 String testImageFileName = testImage.getName();
179 File expectedTextFile = new File(testBase,
180 testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
181 String expectedText = readFileAsString(expectedTextFile);
183 for (int x = 0; x < testCount; x++) {
184 float rotation = testResults.get(x).getRotation();
185 BufferedImage rotatedImage = rotateImage(image, rotation);
186 LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
187 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
188 if (decode(bitmap, rotation, expectedText, false)) {
191 if (decode(bitmap, rotation, expectedText, true)) {
192 tryHarderCounts[x]++;
197 // Print the results of all tests first
199 int totalMustPass = 0;
200 for (int x = 0; x < testCount; x++) {
201 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:");
202 System.out.println(" " + passedCounts[x] + " of " + imageFiles.length + " images passed ("
203 + testResults.get(x).getMustPassCount() + " required)");
204 System.out.println(" " + tryHarderCounts[x] + " of " + imageFiles.length +
205 " images passed with try harder (" + testResults.get(x).getTryHarderCount() +
207 totalFound += passedCounts[x];
208 totalFound += tryHarderCounts[x];
209 totalMustPass += testResults.get(x).getMustPassCount();
210 totalMustPass += testResults.get(x).getTryHarderCount();
213 int totalTests = imageFiles.length * testCount * 2;
214 System.out.println("TOTALS:\n Decoded " + totalFound + " images out of " + totalTests +
215 " (" + (totalFound * 100 / totalTests) + "%)");
216 if (totalFound > totalMustPass) {
217 System.out.println(" *** Test too lax by " + (totalFound - totalMustPass) + " images");
220 // Then run through again and assert if any failed
221 if (assertOnFailure) {
222 for (int x = 0; x < testCount; x++) {
223 assertTrue("Rotation " + testResults.get(x).getRotation() +
224 " degrees: Too many images failed",
225 passedCounts[x] >= testResults.get(x).getMustPassCount());
226 assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
227 " degrees: Too many images failed",
228 tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
231 return new SummaryResults(totalFound, totalMustPass, totalTests);
234 private boolean decode(BinaryBitmap source, float rotation, String expectedText,
237 String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
240 Hashtable<DecodeHintType, Object> hints = getHints();
243 hints = TRY_HARDER_HINT;
245 hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
248 result = barcodeReader.decode(source, hints);
249 } catch (ReaderException re) {
250 System.out.println(re + suffix);
254 if (!expectedFormat.equals(result.getBarcodeFormat())) {
255 System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
256 result.getBarcodeFormat() + '\'' + suffix);
260 String resultText = result.getText();
261 if (!expectedText.equals(resultText)) {
262 System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
269 private static String readFileAsString(File file) throws IOException {
270 StringBuilder result = new StringBuilder((int) file.length());
271 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF8"));
273 char[] buffer = new char[256];
275 while ((charsRead = reader.read(buffer)) > 0) {
276 result.append(buffer, 0, charsRead);
281 return result.toString();
284 protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
285 if (degrees == 0.0f) {
288 AffineTransform at = new AffineTransform();
289 at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0, original.getHeight() / 2.0);
290 BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
291 return op.filter(original, null);