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.BinaryBitmap;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
26 import java.awt.image.BufferedImage;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
32 import javax.imageio.ImageIO;
35 * This abstract class looks for negative results, i.e. it only allows a certain number of false
36 * positives in images which should not decode. This helps ensure that we are not too lenient.
38 * @author dswitkin@google.com (Daniel Switkin)
40 public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
42 private static class TestResult {
43 private final int falsePositivesAllowed;
44 private final float rotation;
46 TestResult(int falsePositivesAllowed, float rotation) {
47 this.falsePositivesAllowed = falsePositivesAllowed;
48 this.rotation = rotation;
51 public int getFalsePositivesAllowed() {
52 return falsePositivesAllowed;
55 public float getRotation() {
60 private final List<TestResult> testResults;
62 // Use the multiformat reader to evaluate all decoders in the system.
63 protected AbstractNegativeBlackBoxTestCase(String testBasePathSuffix) {
64 super(testBasePathSuffix, new MultiFormatReader(), null);
65 testResults = new ArrayList<TestResult>();
68 protected void addTest(int falsePositivesAllowed, float rotation) {
69 testResults.add(new TestResult(falsePositivesAllowed, rotation));
73 public void testBlackBox() throws IOException {
74 assertFalse(testResults.isEmpty());
76 File[] imageFiles = getImageFiles();
77 int[] falsePositives = new int[testResults.size()];
78 for (File testImage : imageFiles) {
79 System.out.println("Starting " + testImage.getAbsolutePath());
81 BufferedImage image = ImageIO.read(testImage);
83 throw new IOException("Could not read image: " + testImage);
85 for (int x = 0; x < testResults.size(); x++) {
86 if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
92 for (int x = 0; x < testResults.size(); x++) {
93 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " +
94 falsePositives[x] + " of " + imageFiles.length + " images were false positives (" +
95 testResults.get(x).getFalsePositivesAllowed() + " allowed)");
96 assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: " +
97 "Too many false positives found",
98 falsePositives[x] <= testResults.get(x).getFalsePositivesAllowed());
103 * Make sure ZXing does NOT find a barcode in the image.
105 * @param image The image to test
106 * @param rotationInDegrees The amount of rotation to apply
107 * @return true if nothing found, false if a non-existant barcode was detected
109 private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
110 BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
111 LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
112 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
115 result = getReader().decode(bitmap);
116 System.out.println("Found false positive: '" + result.getText() + "' with format '" +
117 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
119 } catch (ReaderException re) {
122 // Try "try harder" getMode
124 result = getReader().decode(bitmap, TRY_HARDER_HINT);
125 System.out.println("Try harder found false positive: '" + result.getText() +
126 "' with format '" + result.getBarcodeFormat() + "' (rotation: " +
127 rotationInDegrees + ')');
129 } catch (ReaderException re) {