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.DecodeHintType;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.Reader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
26 import junit.framework.TestCase;
28 import javax.imageio.ImageIO;
29 import java.awt.geom.AffineTransform;
30 import java.awt.image.AffineTransformOp;
31 import java.awt.image.BufferedImage;
32 import java.awt.image.BufferedImageOp;
34 import java.io.FileInputStream;
35 import java.io.FilenameFilter;
36 import java.io.IOException;
37 import java.io.InputStreamReader;
38 import java.util.Hashtable;
39 import java.util.List;
40 import java.util.ArrayList;
41 import java.nio.charset.Charset;
44 * @author srowen@google.com (Sean Owen)
45 * @author dswitkin@google.com (Daniel Switkin)
47 public abstract class AbstractBlackBoxTestCase extends TestCase {
49 protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
51 TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
52 TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
55 private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
56 public boolean accept(File dir, String name) {
57 String lowerCase = name.toLowerCase();
58 return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
59 lowerCase.endsWith(".gif") || lowerCase.endsWith(".png");
63 private static class TestResult {
64 private final int mustPassCount;
65 private final int tryHarderCount;
66 private final float rotation;
68 TestResult(int mustPassCount, int tryHarderCount, float rotation) {
69 this.mustPassCount = mustPassCount;
70 this.tryHarderCount = tryHarderCount;
71 this.rotation = rotation;
73 public int getMustPassCount() {
76 public int getTryHarderCount() {
77 return tryHarderCount;
79 public float getRotation() {
84 private final File testBase;
85 private final Reader barcodeReader;
86 private final BarcodeFormat expectedFormat;
87 private final List<TestResult> testResults;
89 protected AbstractBlackBoxTestCase(File testBase,
91 BarcodeFormat expectedFormat) {
92 this.testBase = testBase;
93 this.barcodeReader = barcodeReader;
94 this.expectedFormat = expectedFormat;
95 testResults = new ArrayList<TestResult>();
99 * Adds a new test for the current directory of images.
101 * @param mustPassCount The number of images which must decode for the test to pass.
102 * @param tryHarderCount The number of images which must pass using the try harder flag.
103 * @param rotation The rotation in degrees clockwise to use for this test.
105 protected void addTest(int mustPassCount, int tryHarderCount, float rotation) {
106 testResults.add(new TestResult(mustPassCount, tryHarderCount, rotation));
109 protected File[] getImageFiles() {
110 assertTrue("Please run from the 'core' directory", testBase.exists());
111 return testBase.listFiles(IMAGE_NAME_FILTER);
114 protected Reader getReader() {
115 return barcodeReader;
118 public void testBlackBox() throws IOException {
119 assertFalse(testResults.isEmpty());
121 File[] imageFiles = getImageFiles();
122 int testCount = testResults.size();
123 int[] passedCounts = new int[testCount];
124 int[] tryHarderCounts = new int[testCount];
125 for (File testImage : imageFiles) {
126 System.out.println("Starting " + testImage.getAbsolutePath());
128 BufferedImage image = ImageIO.read(testImage);
130 String testImageFileName = testImage.getName();
131 File expectedTextFile = new File(testBase,
132 testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
133 String expectedText = readFileAsString(expectedTextFile);
135 for (int x = 0; x < testCount; x++) {
136 float rotation = testResults.get(x).getRotation();
137 BufferedImage rotatedImage = rotateImage(image, rotation);
138 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
139 if (decode(source, rotation, expectedText, false)) {
142 if (decode(source, rotation, expectedText, true)) {
143 tryHarderCounts[x]++;
148 // Print the results of all tests first
149 for (int x = 0; x < testCount; x++) {
150 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:");
151 System.out.println(" " + passedCounts[x] + " of " + imageFiles.length + " images passed ("
152 + testResults.get(x).getMustPassCount() + " required)");
153 System.out.println(" " + tryHarderCounts[x] + " of " + imageFiles.length +
154 " images passed with try harder (" + testResults.get(x).getTryHarderCount() +
158 // Then run through again and assert if any failed
159 for (int x = 0; x < testCount; x++) {
160 assertTrue("Rotation " + testResults.get(x).getRotation() +
161 " degrees: Too many images failed",
162 passedCounts[x] >= testResults.get(x).getMustPassCount());
163 assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
164 " degrees: Too many images failed",
165 tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
169 private boolean decode(MonochromeBitmapSource source, float rotation, String expectedText,
172 String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
175 result = barcodeReader.decode(source, tryHarder ? TRY_HARDER_HINT : null);
176 } catch (ReaderException re) {
177 System.out.println(re + suffix);
181 if (!expectedFormat.equals(result.getBarcodeFormat())) {
182 System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
183 result.getBarcodeFormat() + "'" + suffix);
187 String resultText = result.getText();
188 if (!expectedText.equals(resultText)) {
189 System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
196 private static String readFileAsString(File file) throws IOException {
197 StringBuilder result = new StringBuilder((int) file.length());
198 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF8"));
200 char[] buffer = new char[256];
202 while ((charsRead = reader.read(buffer)) > 0) {
203 result.append(buffer, 0, charsRead);
208 return result.toString();
211 protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
212 if (degrees == 0.0f) {
215 AffineTransform at = new AffineTransform();
216 at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f);
217 BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
218 return op.filter(original, null);