X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Foned%2FCode39Reader.java;h=50c0ede58e5b1612db6017e82379a5d0617b8f61;hb=25232481808167a95813df19c47d088e28922634;hp=36ff24c7ae1b550b0c49580e54ed4f05a9ab4305;hpb=02fec09e579b24c0f41beca4b1aebe21daf8f700;p=zxing.git diff --git a/core/src/com/google/zxing/oned/Code39Reader.java b/core/src/com/google/zxing/oned/Code39Reader.java index 36ff24c7..50c0ede5 100644 --- a/core/src/com/google/zxing/oned/Code39Reader.java +++ b/core/src/com/google/zxing/oned/Code39Reader.java @@ -1,5 +1,5 @@ /* - * Copyright 2008 Google Inc. + * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,22 +17,23 @@ package com.google.zxing.oned; import com.google.zxing.BarcodeFormat; -import com.google.zxing.ReaderException; +import com.google.zxing.ChecksumException; +import com.google.zxing.FormatException; +import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.ResultPoint; import com.google.zxing.common.BitArray; -import com.google.zxing.common.GenericResultPoint; import java.util.Hashtable; /** - *

Decodes Code 39 barcodes. This does not supported "Full ASCII Code 39" yet.

+ *

Decodes Code 39 barcodes. This does not support "Full ASCII Code 39" yet.

* - * @author srowen@google.com (Sean Owen) + * @author Sean Owen */ -public final class Code39Reader extends AbstractOneDReader { +public final class Code39Reader extends OneDReader { - private static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%"; + static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%"; private static final char[] ALPHABET = ALPHABET_STRING.toCharArray(); /** @@ -40,7 +41,7 @@ public final class Code39Reader extends AbstractOneDReader { * The 9 least-significant bits of each int correspond to the pattern of wide and narrow, * with 1s representing "wide" and 0s representing narrow. */ - private static final int[] CHARACTER_ENCODINGS = { + static final int[] CHARACTER_ENCODINGS = { 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, // 0-9 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, // A-J 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, // K-T @@ -89,12 +90,11 @@ public final class Code39Reader extends AbstractOneDReader { this.extendedMode = extendedMode; } - public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException { + public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) + throws NotFoundException, ChecksumException, FormatException { int[] start = findAsteriskPattern(row); - int nextStart = start[1]; - int end = row.getSize(); // Read off white space @@ -102,13 +102,16 @@ public final class Code39Reader extends AbstractOneDReader { nextStart++; } - StringBuffer result = new StringBuffer(); + StringBuffer result = new StringBuffer(20); int[] counters = new int[9]; char decodedChar; int lastStart; do { recordPattern(row, nextStart, counters); int pattern = toNarrowWidePattern(counters); + if (pattern < 0) { + throw NotFoundException.getNotFoundInstance(); + } decodedChar = patternToChar(pattern); result.append(decodedChar); lastStart = nextStart; @@ -122,6 +125,18 @@ public final class Code39Reader extends AbstractOneDReader { } while (decodedChar != '*'); result.deleteCharAt(result.length() - 1); // remove asterisk + // Look for whitespace after pattern: + int lastPatternSize = 0; + for (int i = 0; i < counters.length; i++) { + lastPatternSize += counters[i]; + } + int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; + // If 50% of last pattern size, following last pattern, is not whitespace, fail + // (but if it's whitespace to the very end of the image, that's OK) + if (nextStart != end && whiteSpaceAfterEnd / 2 < lastPatternSize) { + throw NotFoundException.getNotFoundInstance(); + } + if (usingCheckDigit) { int max = result.length() - 1; int total = 0; @@ -129,7 +144,7 @@ public final class Code39Reader extends AbstractOneDReader { total += ALPHABET_STRING.indexOf(result.charAt(i)); } if (total % 43 != ALPHABET_STRING.indexOf(result.charAt(max))) { - throw new ReaderException("Checksum failed"); + throw ChecksumException.getChecksumInstance(); } result.deleteCharAt(max); } @@ -141,7 +156,7 @@ public final class Code39Reader extends AbstractOneDReader { if (resultString.length() == 0) { // Almost surely a false positive - throw new ReaderException("Empty barcode found; assuming a false positive"); + throw NotFoundException.getNotFoundInstance(); } float left = (float) (start[1] + start[0]) / 2.0f; @@ -150,13 +165,13 @@ public final class Code39Reader extends AbstractOneDReader { resultString, null, new ResultPoint[]{ - new GenericResultPoint(left, (float) rowNumber), - new GenericResultPoint(right, (float) rowNumber)}, + new ResultPoint(left, (float) rowNumber), + new ResultPoint(right, (float) rowNumber)}, BarcodeFormat.CODE_39); } - private static int[] findAsteriskPattern(BitArray row) throws ReaderException { + private static int[] findAsteriskPattern(BitArray row) throws NotFoundException { int width = row.getSize(); int rowOffset = 0; while (rowOffset < width) { @@ -174,16 +189,15 @@ public final class Code39Reader extends AbstractOneDReader { for (int i = rowOffset; i < width; i++) { boolean pixel = row.get(i); - if ((!pixel && isWhite) || (pixel && !isWhite)) { + if (pixel ^ isWhite) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { - try { - if (toNarrowWidePattern(counters) == ASTERISK_ENCODING) { + if (toNarrowWidePattern(counters) == ASTERISK_ENCODING) { + // Look for whitespace before start pattern, >= 50% of width of start pattern + if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) { return new int[]{patternStart, i}; } - } catch (ReaderException re) { - // no match, continue } patternStart += counters[0] + counters[1]; for (int y = 2; y < patternLength; y++) { @@ -199,10 +213,12 @@ public final class Code39Reader extends AbstractOneDReader { isWhite = !isWhite; } } - throw new ReaderException("Can't find pattern"); + throw NotFoundException.getNotFoundInstance(); } - private static int toNarrowWidePattern(int[] counters) throws ReaderException { + // For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions + // per image when using some of our blackbox images. + private static int toNarrowWidePattern(int[] counters) { int numCounters = counters.length; int maxNarrowCounter = 0; int wideCounters; @@ -236,26 +252,26 @@ public final class Code39Reader extends AbstractOneDReader { wideCounters--; // totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average if ((counter << 1) >= totalWideCountersWidth) { - throw new ReaderException("Wide bars vary too much in width, rejecting"); + return -1; } } } return pattern; } } while (wideCounters > 3); - throw new ReaderException("Can't find 3 wide bars/spaces out of 9"); + return -1; } - private static char patternToChar(int pattern) throws ReaderException { + private static char patternToChar(int pattern) throws NotFoundException { for (int i = 0; i < CHARACTER_ENCODINGS.length; i++) { if (CHARACTER_ENCODINGS[i] == pattern) { return ALPHABET[i]; } } - throw new ReaderException("Pattern did not match character encoding"); + throw NotFoundException.getNotFoundInstance(); } - private static String decodeExtended(String encoded) throws ReaderException { + private static String decodeExtended(String encoded) throws FormatException { int length = encoded.length(); StringBuffer decoded = new StringBuffer(length); for (int i = 0; i < length; i++) { @@ -269,7 +285,7 @@ public final class Code39Reader extends AbstractOneDReader { if (next >= 'A' && next <= 'Z') { decodedChar = (char) (next + 32); } else { - throw new ReaderException("Invalid extended code 39 sequence: " + c + next); + throw FormatException.getFormatInstance(); } break; case '$': @@ -277,7 +293,7 @@ public final class Code39Reader extends AbstractOneDReader { if (next >= 'A' && next <= 'Z') { decodedChar = (char) (next - 64); } else { - throw new ReaderException("Invalid extended code 39 sequence: " + c + next); + throw FormatException.getFormatInstance(); } break; case '%': @@ -287,7 +303,7 @@ public final class Code39Reader extends AbstractOneDReader { } else if (next >= 'F' && next <= 'W') { decodedChar = (char) (next - 11); } else { - throw new ReaderException("Invalid extended code 39 sequence: " + c + next); + throw FormatException.getFormatInstance(); } break; case '/': @@ -297,7 +313,7 @@ public final class Code39Reader extends AbstractOneDReader { } else if (next == 'Z') { decodedChar = ':'; } else { - throw new ReaderException("Invalid extended sequence: " + c + next); + throw FormatException.getFormatInstance(); } break; }