X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Foned%2FITFReader.java;h=cf68d5845310777d4e8f28f90f4c233ad73429b0;hb=f8bdae4de2d3f78fdb7063726e55c5dce3324e03;hp=676f9b308c3c301b3c45dbde3309657d6790f848;hpb=767bfc87f514b1ce1eb57aeb9149f3ae820bd005;p=zxing.git diff --git a/core/src/com/google/zxing/oned/ITFReader.java b/core/src/com/google/zxing/oned/ITFReader.java index 676f9b30..cf68d584 100644 --- a/core/src/com/google/zxing/oned/ITFReader.java +++ b/core/src/com/google/zxing/oned/ITFReader.java @@ -20,6 +20,7 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.ResultPoint; +import com.google.zxing.DecodeHintType; import com.google.zxing.common.BitArray; import com.google.zxing.common.GenericResultPoint; @@ -45,6 +46,8 @@ public final class ITFReader extends AbstractOneDReader { private static final int W = 3; // Pixel width of a wide line private static final int N = 1; // Pixed width of a narrow line + private static final int[] DEFAULT_ALLOWED_LENGTHS = { 6, 10, 14 }; + // Stores the actual narrow line width of the image being decoded. private int narrowLineWidth = -1; @@ -60,7 +63,7 @@ public final class ITFReader extends AbstractOneDReader { /** * Patterns of Wide / Narrow lines to indicate each digit */ - static final int[][] PATTERNS = { + private static final int[][] PATTERNS = { {N, N, W, W, N}, // 0 {W, N, N, N, W}, // 1 {N, W, N, N, W}, // 2 @@ -73,7 +76,7 @@ public final class ITFReader extends AbstractOneDReader { {N, W, N, W, N} // 9 }; - public final Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException { + public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException { StringBuffer result = new StringBuffer(20); @@ -85,10 +88,23 @@ public final class ITFReader extends AbstractOneDReader { String resultString = result.toString(); + int[] allowedLengths = (int[]) hints.get(DecodeHintType.ALLOWED_LENGTHS); + if (allowedLengths == null) { + allowedLengths = DEFAULT_ALLOWED_LENGTHS; + } + // To avoid false positives with 2D barcodes (and other patterns), make // an assumption that the decoded string must be 6, 10 or 14 digits. int length = resultString.length(); - if (length != 6 && length != 10 && length != 14) { + boolean lengthOK = false; + for (int i = 0; i < allowedLengths.length; i++) { + if (length == allowedLengths[i]) { + lengthOK = true; + break; + } + + } + if (!lengthOK) { throw ReaderException.getInstance(); } @@ -106,7 +122,7 @@ public final class ITFReader extends AbstractOneDReader { * @param resultString {@link StringBuffer} to append decoded chars to * @throws ReaderException if decoding could not complete successfully */ - protected void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuffer resultString) throws ReaderException { + static void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuffer resultString) throws ReaderException { // Digits are interleaved in pairs - 5 black lines for one digit, and the // 5 @@ -129,9 +145,9 @@ public final class ITFReader extends AbstractOneDReader { } int bestMatch = decodeDigit(counterBlack); - resultString.append((char) ('0' + bestMatch % 10)); + resultString.append((char) ('0' + bestMatch)); bestMatch = decodeDigit(counterWhite); - resultString.append((char) ('0' + bestMatch % 10)); + resultString.append((char) ('0' + bestMatch)); for (int i = 0; i < counterDigitPair.length; i++) { payloadStart += counterDigitPair[i]; @@ -149,7 +165,7 @@ public final class ITFReader extends AbstractOneDReader { */ int[] decodeStart(BitArray row) throws ReaderException { int endStart = skipWhiteSpace(row); - int startPattern[] = findGuardPattern(row, endStart, START_PATTERN); + int[] startPattern = findGuardPattern(row, endStart, START_PATTERN); // Determine the width of a narrow line in pixels. We can do this by // getting the width of the start pattern and dividing by 4 because its @@ -199,7 +215,7 @@ public final class ITFReader extends AbstractOneDReader { * @return index of the first black line. * @throws ReaderException Throws exception if no black lines are found in the row */ - private int skipWhiteSpace(BitArray row) throws ReaderException { + private static int skipWhiteSpace(BitArray row) throws ReaderException { int width = row.getSize(); int endStart = 0; while (endStart < width) { @@ -231,7 +247,7 @@ public final class ITFReader extends AbstractOneDReader { row.reverse(); int endStart = skipWhiteSpace(row); - int endPattern[]; + int[] endPattern; try { endPattern = findGuardPattern(row, endStart, END_PATTERN_REVERSED); } catch (ReaderException e) { @@ -266,7 +282,7 @@ public final class ITFReader extends AbstractOneDReader { * ints * @throws ReaderException if pattern is not found */ - int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException { + static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException { // TODO: This is very similar to implementation in AbstractUPCEANReader. Consider if they can be merged to // a single method. @@ -280,7 +296,7 @@ public final class ITFReader extends AbstractOneDReader { int patternStart = rowOffset; for (int x = rowOffset; x < width; x++) { boolean pixel = row.get(x); - if ((!pixel && isWhite) || (pixel && !isWhite)) { + if (pixel ^ isWhite) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { @@ -298,7 +314,7 @@ public final class ITFReader extends AbstractOneDReader { counterPosition++; } counters[counterPosition] = 1; - isWhite = !isWhite; + isWhite ^= true; // isWhite = !isWhite; } } throw ReaderException.getInstance(); @@ -312,7 +328,7 @@ public final class ITFReader extends AbstractOneDReader { * @return The decoded digit * @throws ReaderException if digit cannot be decoded */ - static int decodeDigit(int[] counters) throws ReaderException { + private static int decodeDigit(int[] counters) throws ReaderException { int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept int bestMatch = -1;