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.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;
31 * This test ensures that random, noisy, or unsupported barcode images do not decode.
33 * @author dswitkin@google.com (Daniel Switkin)
35 public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCase {
37 // This number should be reduced as we get better at rejecting false positives.
38 private static final int FALSE_POSITIVES_ALLOWED = 44;
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);
45 public void testBlackBox() throws IOException {
46 File[] imageFiles = getImageFiles();
47 int falsePositives = 0;
48 for (File testImage : imageFiles) {
49 System.out.println("Starting " + testImage.getAbsolutePath());
51 // Try all four rotations, since many of the test images don't have a notion of up, and we
52 // want to be as robust as possible.
53 BufferedImage image = ImageIO.read(testImage);
54 for (int x = 0; x < 4; x++) {
55 if (!checkForFalsePositives(image, x * 90.0f)) {
61 System.out.println("Found " + falsePositives + " false positives (" + FALSE_POSITIVES_ALLOWED +
63 assertTrue("Too many false positives found", falsePositives <= FALSE_POSITIVES_ALLOWED);
67 * Make sure ZXing does NOT find a barcode in the image.
69 * @param image The image to test
70 * @param rotationInDegrees The amount of rotation to apply
71 * @return true if nothing found, false if a non-existant barcode was detected
73 private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
74 BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
75 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
78 result = getReader().decode(source);
79 System.out.println("Found false positive: '" + result.getText() + "' with format '" +
80 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
82 } catch (ReaderException re) {
85 // Try "try harder" mode
87 result = getReader().decode(source, TRY_HARDER_HINT);
88 System.out.println("Try harder found false positive: '" + result.getText() + "' with format '" +
89 result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
91 } catch (ReaderException re) {