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.ResultMetadataType;
27 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
28 import junit.framework.TestCase;
30 import javax.imageio.ImageIO;
31 import java.awt.geom.AffineTransform;
32 import java.awt.image.AffineTransformOp;
33 import java.awt.image.BufferedImage;
34 import java.awt.image.BufferedImageOp;
36 import java.io.FileInputStream;
37 import java.io.FilenameFilter;
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 import java.nio.charset.Charset;
41 import java.util.ArrayList;
42 import java.util.Hashtable;
43 import java.util.List;
45 import java.util.Properties;
49 * @author dswitkin@google.com (Daniel Switkin)
51 public abstract class AbstractBlackBoxTestCase extends TestCase {
53 protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
55 TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
56 TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
59 private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
60 public boolean accept(File dir, String name) {
61 String lowerCase = name.toLowerCase();
62 return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
63 lowerCase.endsWith(".gif") || lowerCase.endsWith(".png");
67 public static class SummaryResults {
68 private int totalFound;
69 private int totalMustPass;
70 private int totalTests;
72 public SummaryResults() {
78 public SummaryResults(int found, int mustPass, int total) {
80 totalMustPass = mustPass;
84 public void add(SummaryResults other) {
85 totalFound += other.totalFound;
86 totalMustPass += other.totalMustPass;
87 totalTests += other.totalTests;
90 public String toString() {
91 return "\nSUMMARY RESULTS:\n Decoded " + totalFound + " images out of " + totalTests +
92 " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)";
96 private static class TestResult {
97 private final int mustPassCount;
98 private final int tryHarderCount;
99 private final float rotation;
101 TestResult(int mustPassCount, int tryHarderCount, float rotation) {
102 this.mustPassCount = mustPassCount;
103 this.tryHarderCount = tryHarderCount;
104 this.rotation = rotation;
106 public int getMustPassCount() {
107 return mustPassCount;
109 public int getTryHarderCount() {
110 return tryHarderCount;
112 public float getRotation() {
117 private final File testBase;
118 private final Reader barcodeReader;
119 private final BarcodeFormat expectedFormat;
120 private final List<TestResult> testResults;
122 protected AbstractBlackBoxTestCase(String testBasePathSuffix,
123 Reader barcodeReader,
124 BarcodeFormat expectedFormat) {
125 // A little workaround to prevent aggravation in my IDE
126 File testBase = new File(testBasePathSuffix);
127 if (!testBase.exists()) {
128 // try starting with 'core' since the test base is often given as the project root
129 testBase = new File("core/" + testBasePathSuffix);
131 this.testBase = testBase;
132 this.barcodeReader = barcodeReader;
133 this.expectedFormat = expectedFormat;
134 testResults = new ArrayList<TestResult>();
138 * Adds a new test for the current directory of images.
140 * @param mustPassCount The number of images which must decode for the test to pass.
141 * @param tryHarderCount The number of images which must pass using the try harder flag.
142 * @param rotation The rotation in degrees clockwise to use for this test.
144 protected void addTest(int mustPassCount, int tryHarderCount, float rotation) {
145 testResults.add(new TestResult(mustPassCount, tryHarderCount, rotation));
148 protected File[] getImageFiles() {
149 assertTrue("Please run from the 'core' directory", testBase.exists());
150 return testBase.listFiles(IMAGE_NAME_FILTER);
153 protected Reader getReader() {
154 return barcodeReader;
157 protected Hashtable<DecodeHintType, Object> getHints() {
161 // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does
162 // not return SummaryResults.
163 public void testBlackBox() throws IOException {
164 testBlackBoxCountingResults(true);
167 public SummaryResults testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
168 assertFalse(testResults.isEmpty());
170 File[] imageFiles = getImageFiles();
171 int testCount = testResults.size();
172 int[] passedCounts = new int[testCount];
173 int[] tryHarderCounts = new int[testCount];
174 for (File testImage : imageFiles) {
175 System.out.println("Starting " + testImage.getAbsolutePath());
177 BufferedImage image = ImageIO.read(testImage);
179 String testImageFileName = testImage.getName();
180 String fileBaseName = testImageFileName.substring(0, testImageFileName.indexOf('.'));
181 File expectedTextFile = new File(testBase, fileBaseName + ".txt");
182 String expectedText = readFileAsString(expectedTextFile);
184 File expectedMetadataFile = new File(testBase, fileBaseName + ".metadata.txt");
185 Properties expectedMetadata = new Properties();
186 if (expectedMetadataFile.exists()) {
187 expectedMetadata.load(new FileInputStream(expectedMetadataFile));
190 for (int x = 0; x < testCount; x++) {
191 float rotation = testResults.get(x).getRotation();
192 BufferedImage rotatedImage = rotateImage(image, rotation);
193 LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
194 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
195 if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) {
198 if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
199 tryHarderCounts[x]++;
204 // Print the results of all tests first
206 int totalMustPass = 0;
207 for (int x = 0; x < testCount; x++) {
208 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:");
209 System.out.println(" " + passedCounts[x] + " of " + imageFiles.length + " images passed ("
210 + testResults.get(x).getMustPassCount() + " required)");
211 System.out.println(" " + tryHarderCounts[x] + " of " + imageFiles.length +
212 " images passed with try harder (" + testResults.get(x).getTryHarderCount() +
214 totalFound += passedCounts[x];
215 totalFound += tryHarderCounts[x];
216 totalMustPass += testResults.get(x).getMustPassCount();
217 totalMustPass += testResults.get(x).getTryHarderCount();
220 int totalTests = imageFiles.length * testCount * 2;
221 System.out.println("TOTALS:\n Decoded " + totalFound + " images out of " + totalTests +
222 " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)");
223 if (totalFound > totalMustPass) {
224 System.out.println(" *** Test too lax by " + (totalFound - totalMustPass) + " images");
225 } else if (totalFound < totalMustPass) {
226 System.out.println(" *** Test failed by " + (totalMustPass - totalFound) + " images");
229 // Then run through again and assert if any failed
230 if (assertOnFailure) {
231 for (int x = 0; x < testCount; x++) {
232 assertTrue("Rotation " + testResults.get(x).getRotation() +
233 " degrees: Too many images failed",
234 passedCounts[x] >= testResults.get(x).getMustPassCount());
235 assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
236 " degrees: Too many images failed",
237 tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
240 return new SummaryResults(totalFound, totalMustPass, totalTests);
243 private boolean decode(BinaryBitmap source,
246 Properties expectedMetadata,
249 String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
252 Hashtable<DecodeHintType, Object> hints = getHints();
255 hints = TRY_HARDER_HINT;
257 hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
260 result = barcodeReader.decode(source, hints);
261 } catch (ReaderException re) {
262 System.out.println(re + suffix);
266 if (!expectedFormat.equals(result.getBarcodeFormat())) {
267 System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
268 result.getBarcodeFormat() + '\'' + suffix);
272 String resultText = result.getText();
273 if (!expectedText.equals(resultText)) {
274 System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
279 Hashtable resultMetadata = result.getResultMetadata();
280 for (Map.Entry<Object,Object> metadatum : expectedMetadata.entrySet()) {
281 ResultMetadataType key = ResultMetadataType.valueOf(metadatum.getKey().toString());
282 Object expectedValue = metadatum.getValue();
283 Object actualValue = resultMetadata == null ? null : resultMetadata.get(key);
284 if (!expectedValue.equals(actualValue)) {
285 System.out.println("Metadata mismatch: for key '" + key + "' expected '" + expectedValue +
286 "' but got '" + actualValue + '\'');
294 private static String readFileAsString(File file) throws IOException {
295 StringBuilder result = new StringBuilder((int) file.length());
296 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF8"));
298 char[] buffer = new char[256];
300 while ((charsRead = reader.read(buffer)) > 0) {
301 result.append(buffer, 0, charsRead);
306 return result.toString();
309 protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
310 if (degrees == 0.0f) {
313 AffineTransform at = new AffineTransform();
314 at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0, original.getHeight() / 2.0);
315 BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
316 return op.filter(original, null);