Added some comments and fixed up lines over 100 columns.
[zxing.git] / core / src / com / google / zxing / oned / AbstractOneDReader.java
index 004eb47..bab29e3 100644 (file)
 
 package com.google.zxing.oned;
 
-import com.google.zxing.BlackPointEstimationMethod;
+import com.google.zxing.BinaryBitmap;
 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.ResultPoint;
 import com.google.zxing.common.BitArray;
-import com.google.zxing.common.GenericResultPoint;
 
 import java.util.Hashtable;
 
@@ -40,24 +38,26 @@ public abstract class AbstractOneDReader implements OneDReader {
   private static final int INTEGER_MATH_SHIFT = 8;
   static final int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT;
 
-  public final Result decode(MonochromeBitmapSource image) throws ReaderException {
+  public final Result decode(BinaryBitmap image) throws ReaderException {
     return decode(image, null);
   }
 
-  public final Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
+  // Note that we don't try rotation without the try harder flag, even if rotation was supported.
+  public final Result decode(BinaryBitmap image, Hashtable hints) throws ReaderException {
     try {
       return doDecode(image, hints);
     } catch (ReaderException re) {
       boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
       if (tryHarder && image.isRotateSupported()) {
-        MonochromeBitmapSource rotatedImage = image.rotateCounterClockwise();
+        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;
+          orientation = (orientation +
+              ((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360;
         }
         result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation));
         return result;
@@ -81,7 +81,7 @@ public abstract class AbstractOneDReader implements OneDReader {
    * @return The contents of the decoded barcode
    * @throws ReaderException Any spontaneous errors which occur
    */
-  private Result doDecode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
+  private Result doDecode(BinaryBitmap image, Hashtable hints) throws ReaderException {
     int width = image.getWidth();
     int height = image.getHeight();
     BitArray row = new BitArray(width);
@@ -109,17 +109,24 @@ public abstract class AbstractOneDReader implements OneDReader {
 
       // Estimate black point for this row and load it:
       try {
-        image.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, rowNumber);
+        row = image.getBlackRow(rowNumber, row);
       } catch (ReaderException re) {
         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
+          // 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)) {
+            hints = (Hashtable) hints.clone();
+            hints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
+          }
         }
         try {
           // Look for a barcode
@@ -130,8 +137,8 @@ public abstract class AbstractOneDReader implements OneDReader {
             result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
             // And remember to flip the result points horizontally.
             ResultPoint[] points = result.getResultPoints();
-            points[0] = new GenericResultPoint(width - points[0].getX() - 1, points[0].getY());
-            points[1] = new GenericResultPoint(width - points[1].getX() - 1, points[1].getY());
+            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) {
@@ -153,7 +160,8 @@ public abstract class AbstractOneDReader implements OneDReader {
    * @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
+   * @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;
@@ -177,7 +185,7 @@ public abstract class AbstractOneDReader implements OneDReader {
           break;
         } else {
           counters[counterPosition] = 1;
-          isWhite ^= true; // isWhite = !isWhite;  Is this too clever? shorter byte code, no conditional
+          isWhite = !isWhite;
         }
       }
       i++;
@@ -229,7 +237,7 @@ public abstract class AbstractOneDReader implements OneDReader {
       if (variance > maxIndividualVariance) {
         return Integer.MAX_VALUE;
       }
-      totalVariance += variance; 
+      totalVariance += variance;
     }
     return totalVariance / total;
   }
@@ -239,6 +247,7 @@ public abstract class AbstractOneDReader implements OneDReader {
   // 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;
+  public abstract Result decodeRow(int rowNumber, BitArray row, Hashtable hints)
+      throws ReaderException;
 
 }