Remove some redundant 'throws'; allocate more reasonably sized StringBuffers for...
[zxing.git] / core / src / com / google / zxing / qrcode / detector / Detector.java
index 9666b9d..9e3cd3d 100644 (file)
@@ -16,8 +16,6 @@
 
 package com.google.zxing.qrcode.detector;
 
-import com.google.zxing.BlackPointEstimationMethod;
-import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.ReaderException;
 import com.google.zxing.ResultPoint;
 import com.google.zxing.common.BitMatrix;
@@ -33,14 +31,18 @@ import java.util.Hashtable;
  *
  * @author Sean Owen
  */
-public final class Detector {
+public class Detector {
 
-  private final MonochromeBitmapSource image;
+  private final BitMatrix image;
 
-  public Detector(MonochromeBitmapSource image) {
+  public Detector(BitMatrix image) {
     this.image = image;
   }
 
+  protected BitMatrix getImage() {
+    return image;
+  }
+
   /**
    * <p>Detects a QR Code in an image, simply.</p>
    *
@@ -60,14 +62,14 @@ public final class Detector {
    */
   public DetectorResult detect(Hashtable hints) throws ReaderException {
 
-    MonochromeBitmapSource image = this.image;
-    if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(image.getLastEstimationMethod())) {
-      image.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);
-    }
-
     FinderPatternFinder finder = new FinderPatternFinder(image);
     FinderPatternInfo info = finder.find(hints);
 
+    return processFinderPatternInfo(info);
+  }
+
+  protected DetectorResult processFinderPatternInfo(FinderPatternInfo info) throws ReaderException {
+
     FinderPattern topLeft = info.getTopLeft();
     FinderPattern topRight = info.getTopRight();
     FinderPattern bottomLeft = info.getBottomLeft();
@@ -106,10 +108,7 @@ public final class Detector {
           // try next round
         }
       }
-      if (alignmentPattern == null) {
-        throw ReaderException.getInstance();
-      }
-
+      // If we didn't find alignment pattern... well try anyway without it
     }
 
     BitMatrix bits = sampleGrid(image, topLeft, topRight, bottomLeft, alignmentPattern, dimension);
@@ -123,7 +122,7 @@ public final class Detector {
     return new DetectorResult(bits, points);
   }
 
-  private static BitMatrix sampleGrid(MonochromeBitmapSource image,
+  private static BitMatrix sampleGrid(BitMatrix image,
                                       ResultPoint topLeft,
                                       ResultPoint topRight,
                                       ResultPoint bottomLeft,
@@ -196,7 +195,8 @@ public final class Detector {
    * <p>Computes an average estimated module size based on estimated derived from the positions
    * of the three finder patterns.</p>
    */
-  private float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft) {
+  private float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight,
+      ResultPoint bottomLeft) {
     // Take the average
     return (calculateModuleSizeOneWay(topLeft, topRight) +
         calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0f;
@@ -287,11 +287,11 @@ public final class Detector {
       int realX = steep ? y : x;
       int realY = steep ? x : y;
       if (state == 1) { // In white pixels, looking for black
-        if (image.isBlack(realX, realY)) {
+        if (image.get(realX, realY)) {
           state++;
         }
       } else {
-        if (!image.isBlack(realX, realY)) {
+        if (!image.get(realX, realY)) {
           state++;
         }
       }
@@ -319,7 +319,7 @@ public final class Detector {
    * @param overallEstModuleSize estimated module size so far
    * @param estAlignmentX x coordinate of center of area probably containing alignment pattern
    * @param estAlignmentY y coordinate of above
-   * @param allowanceFactor number of pixels in all directons to search from the center
+   * @param allowanceFactor number of pixels in all directions to search from the center
    * @return {@link AlignmentPattern} if found, or null otherwise
    * @throws ReaderException if an unexpected error occurs during detection
    */