Alternate multi QR Code reader from Hannes
[zxing.git] / core / src / com / google / zxing / qrcode / detector / Detector.java
index f57ae58..0f28ec9 100644 (file)
@@ -22,7 +22,6 @@ import com.google.zxing.ReaderException;
 import com.google.zxing.ResultPoint;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.common.DetectorResult;
-import com.google.zxing.common.GenericResultPoint;
 import com.google.zxing.common.GridSampler;
 import com.google.zxing.qrcode.decoder.Version;
 
@@ -32,9 +31,9 @@ import java.util.Hashtable;
  * <p>Encapsulates logic that can detect a QR Code in an image, even if the QR Code
  * is rotated or skewed, or partially obscured.</p>
  *
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
-public final class Detector {
+public class Detector {
 
   private final MonochromeBitmapSource image;
 
@@ -42,6 +41,10 @@ public final class Detector {
     this.image = image;
   }
 
+  protected MonochromeBitmapSource getImage() {
+    return image;
+  }
+
   /**
    * <p>Detects a QR Code in an image, simply.</p>
    *
@@ -69,11 +72,19 @@ public final class Detector {
     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();
 
     float moduleSize = calculateModuleSize(topLeft, topRight, bottomLeft);
+    if (moduleSize < 1.0f) {
+      throw ReaderException.getInstance();
+    }
     int dimension = computeDimension(topLeft, topRight, bottomLeft, moduleSize);
     Version provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
     int modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7;
@@ -104,10 +115,7 @@ public final class Detector {
           // try next round
         }
       }
-      if (alignmentPattern == null) {
-        throw new ReaderException("Could not find alignment pattern");
-      }
-
+      // If we didn't find alignment pattern... well try anyway without it
     }
 
     BitMatrix bits = sampleGrid(image, topLeft, topRight, bottomLeft, alignmentPattern, dimension);
@@ -173,8 +181,8 @@ public final class Detector {
                                       ResultPoint topRight,
                                       ResultPoint bottomLeft,
                                       float moduleSize) throws ReaderException {
-    int tltrCentersDimension = round(GenericResultPoint.distance(topLeft, topRight) / moduleSize);
-    int tlblCentersDimension = round(GenericResultPoint.distance(topLeft, bottomLeft) / moduleSize);
+    int tltrCentersDimension = round(ResultPoint.distance(topLeft, topRight) / moduleSize);
+    int tlblCentersDimension = round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
     int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
     switch (dimension & 0x03) { // mod 4
       case 0:
@@ -185,7 +193,7 @@ public final class Detector {
         dimension--;
         break;
       case 3:
-        throw new ReaderException("Bad dimension: " + dimension);
+        throw ReaderException.getInstance();
     }
     return dimension;
   }
@@ -331,6 +339,10 @@ public final class Detector {
     int allowance = (int) (allowanceFactor * overallEstModuleSize);
     int alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance);
     int alignmentAreaRightX = Math.min(image.getWidth() - 1, estAlignmentX + allowance);
+    if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) {
+      throw ReaderException.getInstance();
+    }
+
     int alignmentAreaTopY = Math.max(0, estAlignmentY - allowance);
     int alignmentAreaBottomY = Math.min(image.getHeight() - 1, estAlignmentY + allowance);