Try more possible finder patterns, but be stricter about vetting them. Produces about...
[zxing.git] / core / src / com / google / zxing / qrcode / detector / Detector.java
index 3f20bdb..940476b 100644 (file)
@@ -37,12 +37,12 @@ public final class Detector {
     this.image = image;
   }
 
-    /**
-     * <p>Detects a QR Code in an image, simply.</p>
-     *
-     * @return {@link DetectorResult} encapsulating results of detecting a QR Code
-     * @throws ReaderException if no QR Code can be found
-     */
+  /**
+   * <p>Detects a QR Code in an image, simply.</p>
+   *
+   * @return {@link DetectorResult} encapsulating results of detecting a QR Code
+   * @throws ReaderException if no QR Code can be found
+   */
   public DetectorResult detect() throws ReaderException {
 
     MonochromeBitmapSource image = this.image;
@@ -62,14 +62,14 @@ public final class Detector {
     Version provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
     int modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7;
 
-    // Guess where a "bottom right" finder pattern would have been
-    float bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX();
-    float bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY();
-
     AlignmentPattern alignmentPattern = null;
     // Anything above version 1 has an alignment pattern
     if (provisionalVersion.getAlignmentPatternCenters().length > 0) {
 
+      // Guess where a "bottom right" finder pattern would have been
+      float bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX();
+      float bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY();
+
       // Estimate that alignment pattern is closer by 3 modules
       // from "bottom right" to known top left location
       float correctionToTopLeft = 1.0f - 3.0f / (float) modulesBetweenFPCenters;
@@ -99,9 +99,9 @@ public final class Detector {
 
     ResultPoint[] points;
     if (alignmentPattern == null) {
-      points = new ResultPoint[] { bottomLeft, topLeft, topRight };      
+      points = new ResultPoint[]{bottomLeft, topLeft, topRight};
     } else {
-      points = new ResultPoint[] { bottomLeft, topLeft, topRight, alignmentPattern };
+      points = new ResultPoint[]{bottomLeft, topLeft, topRight, alignmentPattern};
     }
     return new DetectorResult(bits, points);
   }
@@ -121,7 +121,7 @@ public final class Detector {
       case 0:
         dimension++;
         break;
-      // 1? do nothing
+        // 1? do nothing
       case 2:
         dimension--;
         break;
@@ -138,23 +138,23 @@ public final class Detector {
   private float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft) {
     // Take the average
     return (calculateModuleSizeOneWay(topLeft, topRight) +
-            calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0f;
+        calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0f;
   }
 
-    /**
-     * <p>Estimates module size based on two finder patterns -- it uses
-     * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
-     * width of each, measuring along the axis between their centers.</p>
-     */
+  /**
+   * <p>Estimates module size based on two finder patterns -- it uses
+   * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
+   * width of each, measuring along the axis between their centers.</p>
+   */
   private float calculateModuleSizeOneWay(ResultPoint pattern, ResultPoint otherPattern) {
     float moduleSizeEst1 = sizeOfBlackWhiteBlackRunBothWays((int) pattern.getX(),
-                                                            (int) pattern.getY(),
-                                                            (int) otherPattern.getX(),
-                                                            (int) otherPattern.getY());
+        (int) pattern.getY(),
+        (int) otherPattern.getX(),
+        (int) otherPattern.getY());
     float moduleSizeEst2 = sizeOfBlackWhiteBlackRunBothWays((int) otherPattern.getX(),
-                                                            (int) otherPattern.getY(),
-                                                            (int) pattern.getX(),
-                                                            (int) pattern.getY());
+        (int) otherPattern.getY(),
+        (int) pattern.getX(),
+        (int) pattern.getY());
     if (Float.isNaN(moduleSizeEst1)) {
       return moduleSizeEst2;
     }
@@ -178,13 +178,15 @@ public final class Detector {
     // Now count other way -- don't run off image though of course
     int otherToX = fromX - (toX - fromX);
     if (otherToX < 0) {
-      otherToX = 0;
+      // "to" should the be the first value not included, so, the first value off
+      // the edge is -1
+      otherToX = -1;
     } else if (otherToX >= image.getWidth()) {
       otherToX = image.getWidth();
     }
     int otherToY = fromY - (toY - fromY);
     if (otherToY < 0) {
-      otherToY = 0;
+      otherToY = -1;
     } else if (otherToY >= image.getHeight()) {
       otherToY = image.getHeight();
     }