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;
try {
return doDecode(image, hints, tryHarder);
} catch (ReaderException re) {
- if (tryHarder && image.isRotatedSupported()) {
+ if (tryHarder && image.isRotateSupported()) {
MonochromeBitmapSource rotatedImage = image.rotateCounterClockwise();
- return doDecode(rotatedImage, hints, tryHarder);
+ 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;
}
// 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);
- int maxLines = tryHarder ? 15 : 7;
+ 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
+ } else {
+ maxLines = 7;
+ }
+
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, hints);
+ image.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, rowNumber);
} catch (ReaderException re) {
- if (tryHarder) {
- row.reverse(); // try scanning the row backwards
- try {
- return decodeRow(rowNumber, row, hints);
- } catch (ReaderException re2) {
- // continue
+ continue;
+ }
+ image.getBlackRow(rowNumber, row, 0, width);
+
+ // 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
+ }
+ 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));
}
+ 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 {
+ /**
+ * 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 ReaderException if counters cannot be filled entirely from row before running out of pixels
+ */
+ 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;
* @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;
return totalVariance / (float) patternLength;
}
- /**
- * Fast round method.
- *
- * @return argument rounded to nearest int
- */
- protected static int round(float f) {
- return (int) (f + 0.5f);
- }
+ // This declaration should not be necessary, since this class is
+ // abstract and so does not have to provide an implementation for every
+ // method of an interface it implements, but it is causing NoSuchMethodError
+ // issues on some Nokia JVMs. So we add this superfluous declaration:
+
+ public abstract Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException;
}