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.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;
25 import javax.imageio.ImageIO;
26 import java.awt.image.BufferedImage;
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.ArrayList;
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.
36 * @author dswitkin@google.com (Daniel Switkin)
38 public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
40 private static class TestResult {
41 private final int falsePositivesAllowed;
42 private final float rotation;
44 TestResult(int falsePositivesAllowed, float rotation) {
45 this.falsePositivesAllowed = falsePositivesAllowed;
46 this.rotation = rotation;
49 public int getFalsePositivesAllowed() {
50 return falsePositivesAllowed;
53 public float getRotation() {
58 private final List<TestResult> testResults;
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>();
66 protected void addTest(int falsePositivesAllowed, float rotation) {
67 testResults.add(new TestResult(falsePositivesAllowed, rotation));
71 public void testBlackBox() throws IOException {
72 assertFalse(testResults.isEmpty());
74 File[] imageFiles = getImageFiles();
75 int[] falsePositives = new int[testResults.size()];
76 for (File testImage : imageFiles) {
77 System.out.println("Starting " + testImage.getAbsolutePath());
79 BufferedImage image = ImageIO.read(testImage);
81 throw new IOException("Could not read image: " + testImage);
83 for (int x = 0; x < testResults.size(); x++) {
84 if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
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());
101 * Make sure ZXing does NOT find a barcode in the image.
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
107 private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
108 BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
109 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
112 result = getReader().decode(source);
113 System.out.println("Found false positive: '" + result.getText() + "' with format '" +
114 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
116 } catch (ReaderException re) {
119 // Try "try harder" getMode
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 + ')');
125 } catch (ReaderException re) {