X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Foned%2FOneDReader.java;h=bad1f6a08caac654c82e053be46f19af4f829a93;hb=9aeefe94e7fe5485a83f16f4aa1997c610e5804f;hp=ae129572558ebb4393ac663229b0349894259cbe;hpb=7d63fd5b1c4e8136b4ca5833445f9e587c85a400;p=zxing.git diff --git a/core/src/com/google/zxing/oned/OneDReader.java b/core/src/com/google/zxing/oned/OneDReader.java index ae129572..bad1f6a0 100644 --- a/core/src/com/google/zxing/oned/OneDReader.java +++ b/core/src/com/google/zxing/oned/OneDReader.java @@ -16,20 +16,270 @@ package com.google.zxing.oned; +import com.google.zxing.BinaryBitmap; +import com.google.zxing.ChecksumException; +import com.google.zxing.DecodeHintType; +import com.google.zxing.FormatException; +import com.google.zxing.NotFoundException; import com.google.zxing.Reader; import com.google.zxing.ReaderException; import com.google.zxing.Result; +import com.google.zxing.ResultMetadataType; +import com.google.zxing.ResultPoint; import com.google.zxing.common.BitArray; +import java.util.Enumeration; import java.util.Hashtable; /** - *

{@link Reader}s which also implement this interface read one-dimensional barcode - * formats, and expose additional functionality that is specific to this type of barcode.

+ * Encapsulates functionality and implementation that is common to all families + * of one-dimensional barcodes. * + * @author dswitkin@google.com (Daniel Switkin) * @author Sean Owen */ -public interface OneDReader extends Reader { +public abstract class OneDReader implements Reader { + + private static final int INTEGER_MATH_SHIFT = 8; + protected static final int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT; + + public Result decode(BinaryBitmap image) throws NotFoundException, FormatException { + return decode(image, null); + } + + // Note that we don't try rotation without the try harder flag, even if rotation was supported. + public Result decode(BinaryBitmap image, Hashtable hints) throws NotFoundException, FormatException { + try { + return doDecode(image, hints); + } catch (NotFoundException nfe) { + boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); + if (tryHarder && image.isRotateSupported()) { + BinaryBitmap rotatedImage = image.rotateCounterClockwise(); + Result result = doDecode(rotatedImage, hints); + // Record that we found it rotated 90 degrees CCW / 270 degrees CW + Hashtable metadata = result.getResultMetadata(); + int orientation = 270; + if (metadata != null && metadata.containsKey(ResultMetadataType.ORIENTATION)) { + // But if we found it reversed in doDecode(), add in that result here: + orientation = (orientation + + ((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360; + } + result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation)); + // Update result points + ResultPoint[] points = result.getResultPoints(); + int height = rotatedImage.getHeight(); + for (int i = 0; i < points.length; i++) { + points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX()); + } + return result; + } else { + throw nfe; + } + } + } + + public void reset() { + // do nothing + } + + /** + * 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 NotFoundException Any spontaneous errors which occur + */ + private Result doDecode(BinaryBitmap image, Hashtable hints) throws NotFoundException { + int width = image.getWidth(); + int height = image.getHeight(); + BitArray row = new BitArray(width); + + int middle = height >> 1; + boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); + int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5)); + int maxLines; + if (tryHarder) { + maxLines = height; // Look at the whole image, not just the center + } else { + maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image + } + + for (int x = 0; x < maxLines; x++) { + + // Scanning from the middle out. Determine which row we're looking at next: + int rowStepsAboveOrBelow = (x + 1) >> 1; + boolean isAbove = (x & 0x01) == 0; // i.e. is x even? + int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); + if (rowNumber < 0 || rowNumber >= height) { + // Oops, if we run off the top or bottom, stop + break; + } + + // Estimate black point for this row and load it: + try { + row = image.getBlackRow(rowNumber, row); + } catch (NotFoundException nfe) { + continue; + } + + // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to + // handle decoding upside down barcodes. + for (int attempt = 0; attempt < 2; attempt++) { + if (attempt == 1) { // trying again? + row.reverse(); // reverse the row and continue + // This means we will only ever draw result points *once* in the life of this method + // since we want to avoid drawing the wrong points after flipping the row, and, + // don't want to clutter with noise from every single row scan -- just the scans + // that start on the center line. + if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) { + Hashtable newHints = new Hashtable(); // Can't use clone() in J2ME + Enumeration hintEnum = hints.keys(); + while (hintEnum.hasMoreElements()) { + Object key = hintEnum.nextElement(); + if (!key.equals(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) { + newHints.put(key, hints.get(key)); + } + } + hints = newHints; + } + } + try { + // Look for a barcode + Result result = decodeRow(rowNumber, row, hints); + // We found our barcode + if (attempt == 1) { + // But it was upside down, so note that + result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180)); + // And remember to flip the result points horizontally. + ResultPoint[] points = result.getResultPoints(); + points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY()); + points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY()); + } + return result; + } catch (ReaderException re) { + // continue -- just couldn't decode this row + } + } + } + + throw NotFoundException.getNotFoundInstance(); + } + + /** + * Records the size of successive runs of white and black pixels in a row, starting at a given point. + * The values are recorded in the given array, and the number of runs recorded is equal to the size + * of the array. If the row starts on a white pixel at the given start point, then the first count + * recorded is the run of white pixels starting from that point; likewise it is the count of a run + * of black pixels if the row begin on a black pixels at that point. + * + * @param row row to count from + * @param start offset into row to start at + * @param counters array into which to record counts + * @throws NotFoundException if counters cannot be filled entirely from row before running out + * of pixels + */ + protected static void recordPattern(BitArray row, int start, int[] counters) throws NotFoundException { + int numCounters = counters.length; + for (int i = 0; i < numCounters; i++) { + counters[i] = 0; + } + int end = row.getSize(); + if (start >= end) { + throw NotFoundException.getNotFoundInstance(); + } + boolean isWhite = !row.get(start); + int counterPosition = 0; + int i = start; + while (i < end) { + boolean pixel = row.get(i); + if (pixel ^ isWhite) { // that is, exactly one is true + counters[counterPosition]++; + } else { + counterPosition++; + if (counterPosition == numCounters) { + break; + } else { + counters[counterPosition] = 1; + isWhite = !isWhite; + } + } + i++; + } + // If we read fully the last section of pixels and filled up our counters -- or filled + // the last counter but ran off the side of the image, OK. Otherwise, a problem. + if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) { + throw NotFoundException.getNotFoundInstance(); + } + } + + protected static void recordPatternInReverse(BitArray row, int start, int[] counters) + throws NotFoundException { + // This could be more efficient I guess + int numTransitionsLeft = counters.length; + boolean last = row.get(start); + while (start > 0 && numTransitionsLeft >= 0) { + if (row.get(--start) != last) { + numTransitionsLeft--; + last = !last; + } + } + if (numTransitionsLeft >= 0) { + throw NotFoundException.getNotFoundInstance(); + } + recordPattern(row, start + 1, counters); + } + + /** + * 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. + * + * @param counters observed counters + * @param pattern expected pattern + * @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 + * even more variance + */ + protected static int patternMatchVariance(int[] counters, int[] pattern, int maxIndividualVariance) { + int numCounters = counters.length; + int total = 0; + int patternLength = 0; + for (int i = 0; i < numCounters; i++) { + total += counters[i]; + patternLength += pattern[i]; + } + if (total < patternLength) { + // If we don't even have one pixel per unit of bar width, assume this is too small + // to reliably match, so fail: + return Integer.MAX_VALUE; + } + // We're going to fake floating-point math in integers. We just need to use more bits. + // Scale up patternLength so that intermediate values below like scaledCounter will have + // more "significant digits" + int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength; + maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT; + + int totalVariance = 0; + for (int x = 0; x < numCounters; x++) { + int counter = counters[x] << INTEGER_MATH_SHIFT; + int scaledPattern = pattern[x] * unitBarWidth; + int variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter; + if (variance > maxIndividualVariance) { + return Integer.MAX_VALUE; + } + totalVariance += variance; + } + return totalVariance / total; + } /** *

Attempts to decode a one-dimensional barcode format given a single row of @@ -39,8 +289,9 @@ public interface OneDReader extends Reader { * @param row the black/white pixel data of the row * @param hints decode hints * @return {@link Result} containing encoded string and start/end of barcode - * @throws ReaderException if an error occurs or barcode cannot be found + * @throws NotFoundException if an error occurs or barcode cannot be found */ - Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException; + public abstract Result decodeRow(int rowNumber, BitArray row, Hashtable hints) + throws NotFoundException, ChecksumException, FormatException; -} \ No newline at end of file +}