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 javax.imageio.ImageIO;
27 import java.awt.image.BufferedImage;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
34 * This abstract class looks for negative results, i.e. it only allows a certain number of false
35 * positives in images which should not decode. This helps ensure that we are not too lenient.
37 * @author dswitkin@google.com (Daniel Switkin)
39 public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
41 private static class TestResult {
42 private final int falsePositivesAllowed;
43 private final float rotation;
45 TestResult(int falsePositivesAllowed, float rotation) {
46 this.falsePositivesAllowed = falsePositivesAllowed;
47 this.rotation = rotation;
50 public int getFalsePositivesAllowed() {
51 return falsePositivesAllowed;
54 public float getRotation() {
59 private final List<TestResult> testResults;
61 // Use the multiformat reader to evaluate all decoders in the system.
62 protected AbstractNegativeBlackBoxTestCase(String testBasePathSuffix) {
63 super(testBasePathSuffix, new MultiFormatReader(), null);
64 testResults = new ArrayList<TestResult>();
67 protected void addTest(int falsePositivesAllowed, float rotation) {
68 testResults.add(new TestResult(falsePositivesAllowed, rotation));
72 public void testBlackBox() throws IOException {
73 assertFalse(testResults.isEmpty());
75 File[] imageFiles = getImageFiles();
76 int[] falsePositives = new int[testResults.size()];
77 for (File testImage : imageFiles) {
78 System.out.println("Starting " + testImage.getAbsolutePath());
80 BufferedImage image = ImageIO.read(testImage);
82 throw new IOException("Could not read image: " + testImage);
84 for (int x = 0; x < testResults.size(); x++) {
85 if (!checkForFalsePositives(image, testResults.get(x).getRotation())) {
91 for (int x = 0; x < testResults.size(); x++) {
92 System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " +
93 falsePositives[x] + " of " + imageFiles.length + " images were false positives (" +
94 testResults.get(x).getFalsePositivesAllowed() + " allowed)");
95 assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: " +
96 "Too many false positives found",
97 falsePositives[x] <= testResults.get(x).getFalsePositivesAllowed());
102 * Make sure ZXing does NOT find a barcode in the image.
104 * @param image The image to test
105 * @param rotationInDegrees The amount of rotation to apply
106 * @return true if nothing found, false if a non-existant barcode was detected
108 private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
109 BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
110 LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
111 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
114 result = getReader().decode(bitmap);
115 System.out.println("Found false positive: '" + result.getText() + "' with format '" +
116 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
118 } catch (ReaderException re) {
121 // Try "try harder" getMode
123 result = getReader().decode(bitmap, TRY_HARDER_HINT);
124 System.out.println("Try harder found false positive: '" + result.getText() +
125 "' with format '" + result.getBarcodeFormat() + "' (rotation: " +
126 rotationInDegrees + ')');
128 } catch (ReaderException re) {