2 * Copyright 2008 Google Inc.
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.Vector;
42 * @author srowen@google.com (Sean Owen)
43 * @author dswitkin@google.com (Daniel Switkin)
45 public abstract class AbstractBlackBoxTestCase extends TestCase {
47 private static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
49 TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
50 TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
53 private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
54 public boolean accept(File dir, String name) {
55 String lowerCase = name.toLowerCase();
56 return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
57 lowerCase.endsWith(".gif") || lowerCase.endsWith(".png");
61 private static class TestResult {
62 private final int mustPassCount;
63 private final float rotation;
64 TestResult(int mustPassCount, float rotation) {
65 this.mustPassCount = mustPassCount;
66 this.rotation = rotation;
68 public int getMustPassCount() {
71 public float getRotation() {
76 private final File testBase;
77 private final Reader barcodeReader;
78 private final BarcodeFormat expectedFormat;
79 private Vector<TestResult> testResults;
81 protected AbstractBlackBoxTestCase(File testBase,
83 BarcodeFormat expectedFormat) {
84 this.testBase = testBase;
85 this.barcodeReader = barcodeReader;
86 this.expectedFormat = expectedFormat;
87 testResults = new Vector<TestResult>();
91 * Adds a new test for the current directory of images.
93 * @param mustPassCount The number of images which must decode for the test to pass.
94 * @param rotation The rotation in degrees clockwise to use for this test.
96 protected void addTest(int mustPassCount, float rotation) {
97 testResults.add(new TestResult(mustPassCount, rotation));
100 public void testBlackBox() throws IOException {
101 assertFalse(testResults.isEmpty());
102 assertTrue("Please run from the 'core' directory", testBase.exists());
104 File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
105 int[] passedCounts = new int[testResults.size()];
106 for (File testImage : imageFiles) {
107 System.out.println("Starting " + testImage.getAbsolutePath());
109 BufferedImage image = ImageIO.read(testImage);
111 String testImageFileName = testImage.getName();
112 File expectedTextFile = new File(testBase,
113 testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
114 String expectedText = readFileAsString(expectedTextFile);
116 for (int x = 0; x < testResults.size(); x++) {
117 if (doTestOneImage(image, testResults.get(x).getRotation(), expectedText)) {
123 for (int x = 0; x < testResults.size(); x++) {
124 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " + passedCounts[x] +
125 " of " + imageFiles.length + " images passed (" + testResults.get(x).getMustPassCount() +
127 assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: Too many images failed",
128 passedCounts[x] >= testResults.get(x).getMustPassCount());
132 private boolean doTestOneImage(BufferedImage image, float rotationInDegrees, String expectedText) {
133 BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
134 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
137 result = barcodeReader.decode(source);
138 } catch (ReaderException re) {
139 System.out.println(re);
143 if (!expectedFormat.equals(result.getBarcodeFormat())) {
144 System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
145 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
149 String resultText = result.getText();
150 if (!expectedText.equals(resultText)) {
151 System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
152 "' (rotation: " + rotationInDegrees + ')');
156 // Try "try harder" mode
158 result = barcodeReader.decode(source, TRY_HARDER_HINT);
159 } catch (ReaderException re) {
160 fail("Normal mode succeeded but \"try harder\" failed");
163 if (!expectedFormat.equals(result.getBarcodeFormat())) {
164 System.out.println("Try Harder Format mismatch: expected '" + expectedFormat + "' but got '" +
165 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
166 } else if (!expectedText.equals(resultText)) {
167 System.out.println("Try Harder Mismatch: expected '" + expectedText + "' but got '" +
168 resultText + "' (rotation: " + rotationInDegrees + ')');
173 private static String readFileAsString(File file) throws IOException {
174 StringBuilder result = new StringBuilder((int) file.length());
175 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
177 char[] buffer = new char[256];
179 while ((charsRead = reader.read(buffer)) > 0) {
180 result.append(buffer, 0, charsRead);
185 return result.toString();
188 private static BufferedImage rotateImage(BufferedImage original, float degrees) {
189 if (degrees == 0.0f) {
192 AffineTransform at = new AffineTransform();
193 at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f);
194 BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
195 return op.filter(original, null);