From a8ab7d5be2768d8e11779bc36886e260e8a2e79c Mon Sep 17 00:00:00 2001 From: srowen Date: Wed, 18 Jun 2008 21:56:30 +0000 Subject: [PATCH] Fix pretty clear bug on Code 128 and 39 readers: reject empty barcodes as a false positive. git-svn-id: http://zxing.googlecode.com/svn/trunk@440 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- core/src/com/google/zxing/oned/Code128Reader.java | 6 ++++++ core/src/com/google/zxing/oned/Code39Reader.java | 6 ++++++ .../google/zxing/common/FalsePositivesBlackBoxTestCase.java | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/core/src/com/google/zxing/oned/Code128Reader.java b/core/src/com/google/zxing/oned/Code128Reader.java index eec326ef..1fc430c3 100644 --- a/core/src/com/google/zxing/oned/Code128Reader.java +++ b/core/src/com/google/zxing/oned/Code128Reader.java @@ -405,6 +405,12 @@ public final class Code128Reader extends AbstractOneDReader { } String resultString = result.toString(); + + if (resultString.length() == 0) { + // Almost surely a false positive + throw new ReaderException("Empty barcode found; assuming a false positive"); + } + float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f; float right = (float) (nextStart + lastStart) / 2.0f; return new Result( diff --git a/core/src/com/google/zxing/oned/Code39Reader.java b/core/src/com/google/zxing/oned/Code39Reader.java index 28113128..12f36b1e 100644 --- a/core/src/com/google/zxing/oned/Code39Reader.java +++ b/core/src/com/google/zxing/oned/Code39Reader.java @@ -138,6 +138,12 @@ public final class Code39Reader extends AbstractOneDReader { if (extendedMode) { resultString = decodeExtended(resultString); } + + if (resultString.length() == 0) { + // Almost surely a false positive + throw new ReaderException("Empty barcode found; assuming a false positive"); + } + float left = (float) (start[1] + start[0]) / 2.0f; float right = (float) (nextStart + lastStart) / 2.0f; return new Result( diff --git a/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java b/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java index a623d11c..d078c15f 100644 --- a/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java +++ b/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java @@ -42,6 +42,7 @@ public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCa super(new File("test/data/blackbox/falsepositives"), new MultiFormatReader(), null); } + @Override public void testBlackBox() throws IOException { File[] imageFiles = getImageFiles(); int falsePositives = 0; @@ -51,6 +52,9 @@ public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCa // Try all four rotations, since many of the test images don't have a notion of up, and we // want to be as robust as possible. BufferedImage image = ImageIO.read(testImage); + if (image == null) { + throw new IOException("Could not read image: " + testImage); + } for (int x = 0; x < 4; x++) { if (!checkForFalsePositives(image, x * 90.0f)) { falsePositives++; -- 2.20.1