package com.google.zxing.oned;
import com.google.zxing.BlackPointEstimationMethod;
+import com.google.zxing.DecodeHintType;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
+import com.google.zxing.ResultMetadataType;
import com.google.zxing.common.BitArray;
import java.util.Hashtable;
}
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);
+ } catch (ReaderException re) {
+ if (tryHarder && image.isRotateSupported()) {
+ MonochromeBitmapSource rotatedImage = image.rotateCounterClockwise();
+ Result result = doDecode(rotatedImage, hints, tryHarder);
+ // 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));
+ return result;
+ } else {
+ throw re;
+ }
+ }
+ }
+
+ private Result doDecode(MonochromeBitmapSource image, Hashtable hints, boolean tryHarder) throws ReaderException {
int width = image.getWidth();
int height = image.getHeight();
BitArray row = new BitArray(width);
+ //int barcodesToSkip = 0;
+ //if (hints != null) {
+ // Integer number = (Integer) hints.get(DecodeHintType.SKIP_N_BARCODES);
+ // if (number != null) {
+ // barcodesToSkip = number.intValue();
+ // }
+ //}
+
// 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.
+ // 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;
- int rowStep = Math.max(1, height >> 4);
- for (int x = 0; x < 7; x++) {
+ int rowStep = Math.max(1, height >> (tryHarder ? 7 : 4));
+ int maxLines;
+ //if (tryHarder || barcodesToSkip > 0) {
+ if (tryHarder) {
+ maxLines = height; // Look at the whole image; looking for more than one barcode
+ } else {
+ maxLines = 7;
+ }
+
+ //Result lastResult = null;
+
+ 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;
}
- image.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, rowNumber);
- image.getBlackRow(rowNumber, row, 0, width);
-
+ // Estimate black point for this row and load it:
try {
- return decodeRow(rowNumber, row);
+ image.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, rowNumber);
} catch (ReaderException re) {
- // TODO re-enable this in a "try harder" mode?
- //row.reverse(); // try scanning the row backwards
- //try {
- // return decodeRow(rowNumber, row);
- //} catch (ReaderException re2) {
- // continue
- //}
+ continue;
}
+ image.getBlackRow(rowNumber, row, 0, width);
+ // We may try twice for each row, if "trying harder":
+ for (int attempt = 0; attempt < 2; attempt++) {
+
+ if (attempt == 1) { // trying again?
+ if (tryHarder) { // only if "trying harder"
+ row.reverse(); // reverse the row and continue
+ } else {
+ break;
+ }
+ }
+
+ try {
+
+ // Look for a barcode
+ Result result = decodeRow(rowNumber, row, hints);
+
+ //if (lastResult != null && lastResult.getText().equals(result.getText())) {
+ // Just saw the last barcode again, proceed
+ //continue;
+ //}
+
+ //if (barcodesToSkip > 0) { // See if we should skip and keep looking
+ // barcodesToSkip--;
+ // lastResult = result; // Remember what we just saw
+ //} else {
+ // We found our barcode
+ if (attempt == 1) {
+ // But it was upside down, so note that
+ result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
+ }
+ return result;
+ //}
+
+ } catch (ReaderException re) {
+ // continue -- just couldn't decode this row
+ }
+
+ }
}
throw new ReaderException("No barcode found");
}
- protected static void recordPattern(BitArray row, int start, int[] counters) throws ReaderException {
- for (int i = 0; i < counters.length; i++) {
+ static void recordPattern(BitArray row, int start, int[] counters) throws ReaderException {
+ int numCounters = counters.length;
+ for (int i = 0; i < numCounters; i++) {
counters[i] = 0;
}
int end = row.getSize();
counters[counterPosition]++;
} else {
counterPosition++;
- if (counterPosition == counters.length) {
+ if (counterPosition == numCounters) {
break;
} else {
counters[counterPosition] = 1;
}
// 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 == counters.length || (counterPosition == counters.length - 1 && i == end))) {
+ if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) {
throw new ReaderException("Couldn't fully read a pattern");
}
}
/**
* Determines how closely a set of observed counts of runs of black/white values matches a given
- * target pattern. For each counter, the ratio of the difference between it and the pattern value
- * is compared to the expected pattern value. This ratio is averaged across counters to produce
- * the return value. 0.0 means an exact match; higher values mean poorer matches.
+ * 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
* @return average variance between counters and pattern
*/
- protected static float patternMatchVariance(int[] counters, int[] pattern) {
+ static float patternMatchVariance(int[] counters, int[] pattern) {
int total = 0;
int numCounters = counters.length;
int patternLength = 0;
float scaledCounter = (float) counters[x] / unitBarWidth;
float width = pattern[x];
float abs = scaledCounter > width ? scaledCounter - width : width - scaledCounter;
- totalVariance += abs / width;
+ totalVariance += abs;
}
- return totalVariance / (float) numCounters;
- }
-
- /**
- * Fast round method.
- *
- * @return argument rounded to nearest int
- */
- protected static int round(float f) {
- return (int) (f + 0.5f);
+ return totalVariance / (float) patternLength;
}
}