Improved approach to 1D decoding -- better use of integer math by scaling pattern...
[zxing.git] / core / test / src / com / google / zxing / common / FalsePositivesBlackBoxTestCase.java
1 /*
2  * Copyright 2008 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.common;
18
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;
24
25 import javax.imageio.ImageIO;
26 import java.awt.image.BufferedImage;
27 import java.io.File;
28 import java.io.IOException;
29
30 /**
31  * This test ensures that random, noisy, or unsupported barcode images do not decode.
32  *
33  * @author dswitkin@google.com (Daniel Switkin)
34  */
35 public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCase {
36
37   // This number should be reduced as we get better at rejecting false positives.
38   private static final int FALSE_POSITIVES_ALLOWED = 23;
39
40   // Use the multiformat reader to evaluate all decoders in the system.
41   public FalsePositivesBlackBoxTestCase() {
42     super(new File("test/data/blackbox/falsepositives"), new MultiFormatReader(), null);
43   }
44
45   @Override
46   public void testBlackBox() throws IOException {
47     File[] imageFiles = getImageFiles();
48     int falsePositives = 0;
49     for (File testImage : imageFiles) {
50       System.out.println("Starting " + testImage.getAbsolutePath());
51
52       // Try all four rotations, since many of the test images don't have a notion of up, and we
53       // want to be as robust as possible.
54       BufferedImage image = ImageIO.read(testImage);
55       if (image == null) {
56         throw new IOException("Could not read image: " + testImage);
57       }
58       for (int x = 0; x < 4; x++) {
59         if (!checkForFalsePositives(image, x * 90.0f)) {
60           falsePositives++;
61         }
62       }
63     }
64
65     System.out.println("Found " + falsePositives + " false positives (" + FALSE_POSITIVES_ALLOWED +
66         " max)");
67     assertTrue("Too many false positives found", falsePositives <= FALSE_POSITIVES_ALLOWED);
68   }
69
70   /**
71    * Make sure ZXing does NOT find a barcode in the image.
72    *
73    * @param image The image to test
74    * @param rotationInDegrees The amount of rotation to apply
75    * @return true if nothing found, false if a non-existant barcode was detected
76    */
77   private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
78     BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
79     MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
80     Result result;
81     try {
82       result = getReader().decode(source);
83       System.out.println("Found false positive: '" + result.getText() + "' with format '" +
84           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
85       return false;
86     } catch (ReaderException re) {
87     }
88
89     // Try "try harder" mode
90     try {
91       result = getReader().decode(source, TRY_HARDER_HINT);
92       System.out.println("Try harder found false positive: '" + result.getText() + "' with format '" +
93           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
94       return false;
95     } catch (ReaderException re) {
96     }
97     return true;
98   }
99
100 }