diff --git a/core/src/com/google/zxing/oned/AbstractOneDReader.java b/core/src/com/google/zxing/oned/AbstractOneDReader.java index 042432e8d..63d4ce87c 100644 --- a/core/src/com/google/zxing/oned/AbstractOneDReader.java +++ b/core/src/com/google/zxing/oned/AbstractOneDReader.java @@ -36,20 +36,20 @@ import java.util.Hashtable; public abstract class AbstractOneDReader implements OneDReader { private static final int INTEGER_MATH_SHIFT = 8; - public static final int PATTERN_MATCH_RESULT_SCALE_FACTOR = 256; + public static final int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT; public final Result decode(MonochromeBitmapSource image) throws ReaderException { return decode(image, null); } public final Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException { - boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); try { - return doDecode(image, hints, tryHarder); + return doDecode(image, hints); } catch (ReaderException re) { + boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); if (tryHarder && image.isRotateSupported()) { MonochromeBitmapSource rotatedImage = image.rotateCounterClockwise(); - Result result = doDecode(rotatedImage, hints, tryHarder); + Result result = doDecode(rotatedImage, hints); // Record that we found it rotated 90 degrees CCW / 270 degrees CW Hashtable metadata = result.getResultMetadata(); int orientation = 270; @@ -65,27 +65,33 @@ public abstract class AbstractOneDReader implements OneDReader { } } - private Result doDecode(MonochromeBitmapSource image, Hashtable hints, boolean tryHarder) throws ReaderException { - + /** + * We're going to examine rows from the middle outward, searching alternately above and below the + * middle, and farther out each time. rowStep is the number of rows between each successive + * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then + * middle + rowStep, then middle - (2 * rowStep), etc. + * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily + * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the + * image if "trying harder". + * + * @param image The image to decode + * @param hints Any hints that were requested + * @return The contents of the decoded barcode + * @throws ReaderException Any spontaneous errors which occur + */ + private Result doDecode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException { int width = image.getWidth(); int height = image.getHeight(); - BitArray row = new BitArray(width); - // We're going to examine rows from the middle outward, searching alternately above and below the middle, - // and farther out each time. rowStep is the number of rows between each successive attempt above and below - // the middle. So we'd scan row middle, then middle - rowStep, then middle + rowStep, - // then middle - 2*rowStep, etc. - // rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily decided - // that moving up and down by about 1/16 of the image is pretty good; we try more of the image if - // "trying harder" int middle = height >> 1; + boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); int rowStep = Math.max(1, height >> (tryHarder ? 7 : 4)); int maxLines; if (tryHarder) { - maxLines = height; // Look at the whole image; looking for more than one barcode + maxLines = height; // Look at the whole image, not just the center } else { - maxLines = 7; + maxLines = 9; // Nine rows spaced 1/16 apart is roughly the middle half of the image } for (int x = 0; x < maxLines; x++) { @@ -179,12 +185,12 @@ public abstract class AbstractOneDReader implements OneDReader { /** * Determines how closely a set of observed counts of runs of black/white values matches a given - * target pattern. This is reported as the ratio of the total variance from the expected pattern proportions - * across all pattern elements, to the length of the pattern. + * target pattern. This is reported as the ratio of the total variance from the expected pattern + * proportions across all pattern elements, to the length of the pattern. * * @param counters observed counters * @param pattern expected pattern - * @param maxIndividualVariance + * @param maxIndividualVariance The most any counter can differ before we give up * @return ratio of total variance between counters and pattern compared to total pattern size, * where the ratio has been multiplied by 256. So, 0 means no variance (perfect match); 256 means * the total variance between counters and patterns equals the pattern length, higher values mean @@ -207,7 +213,7 @@ public abstract class AbstractOneDReader implements OneDReader { // Scale up patternLength so that intermediate values below like scaledCounter will have // more "significant digits" int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength; - maxIndividualVariance *= unitBarWidth; + maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT; int totalVariance = 0; for (int x = 0; x < numCounters; x++) { diff --git a/core/src/com/google/zxing/oned/AbstractUPCEANReader.java b/core/src/com/google/zxing/oned/AbstractUPCEANReader.java index 7b72953cb..96df094be 100644 --- a/core/src/com/google/zxing/oned/AbstractUPCEANReader.java +++ b/core/src/com/google/zxing/oned/AbstractUPCEANReader.java @@ -36,7 +36,7 @@ import java.util.Hashtable; public abstract class AbstractUPCEANReader extends AbstractOneDReader implements UPCEANReader { private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.40625f); - private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.5f); + private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f); /** * Start/end guard pattern. @@ -140,8 +140,10 @@ public abstract class AbstractUPCEANReader extends AbstractOneDReader implements * @throws ReaderException if the string does not contain only digits */ boolean checkChecksum(String s) throws ReaderException { - int sum = 0; int length = s.length(); + if (length == 0) return false; + + int sum = 0; for (int i = length - 2; i >= 0; i -= 2) { int digit = (int) s.charAt(i) - (int) '0'; if (digit < 0 || digit > 9) { diff --git a/core/src/com/google/zxing/oned/Code128Reader.java b/core/src/com/google/zxing/oned/Code128Reader.java index 280619ae6..15508bb07 100644 --- a/core/src/com/google/zxing/oned/Code128Reader.java +++ b/core/src/com/google/zxing/oned/Code128Reader.java @@ -143,7 +143,7 @@ public final class Code128Reader extends AbstractOneDReader { }; private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.1875f); - private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.25f); + private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.35f); private static final int CODE_SHIFT = 98; diff --git a/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java b/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java index ad40cd4d9..771798c04 100644 --- a/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java +++ b/core/test/src/com/google/zxing/common/FalsePositivesBlackBoxTestCase.java @@ -35,7 +35,7 @@ import java.io.IOException; public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCase { // This number should be reduced as we get better at rejecting false positives. - private static final int FALSE_POSITIVES_ALLOWED = 15; + private static final int FALSE_POSITIVES_ALLOWED = 4; // Use the multiformat reader to evaluate all decoders in the system. public FalsePositivesBlackBoxTestCase() { diff --git a/core/test/src/com/google/zxing/oned/EAN8BlackBox1TestCase.java b/core/test/src/com/google/zxing/oned/EAN8BlackBox1TestCase.java index 290ae2cf7..e8e2b993f 100644 --- a/core/test/src/com/google/zxing/oned/EAN8BlackBox1TestCase.java +++ b/core/test/src/com/google/zxing/oned/EAN8BlackBox1TestCase.java @@ -30,7 +30,7 @@ public final class EAN8BlackBox1TestCase extends AbstractBlackBoxTestCase { public EAN8BlackBox1TestCase() { super(new File("test/data/blackbox/ean8-1"), new MultiFormatReader(), BarcodeFormat.EAN_8); addTest(8, 0.0f); - addTest(7, 180.0f); + addTest(8, 180.0f); } } \ No newline at end of file