Adjust formatting on last change. Simplify GridSampler
[zxing.git] / core / src / com / google / zxing / qrcode / detector / Detector.java
index 82d50a6..b0eba53 100644 (file)
@@ -160,7 +160,7 @@ public class Detector {
       sourceBottomRightX = sourceBottomRightY = dimMinusThree;
     }
 
-    PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
+    return PerspectiveTransform.quadrilateralToQuadrilateral(
         3.5f,
         3.5f,
         dimMinusThree,
@@ -177,8 +177,6 @@ public class Detector {
         bottomRightY,
         bottomLeft.getX(),
         bottomLeft.getY());
-
-    return transform;
   }
 
   private static BitMatrix sampleGrid(BitMatrix image,
@@ -186,7 +184,7 @@ public class Detector {
                                       int dimension) throws NotFoundException {
 
     GridSampler sampler = GridSampler.getInstance();
-    return sampler.sampleGrid(image, dimension, transform);
+    return sampler.sampleGrid(image, dimension, dimension, transform);
   }
 
   /**
@@ -266,9 +264,9 @@ public class Detector {
    if (otherToX < 0) {
      scale = (float) fromX / (float) (fromX - otherToX);
      otherToX = 0;
-   } else if (otherToX >= image.getWidth()) {
-     scale = (float) (image.getWidth() - 1 - fromX) / (float) (otherToX - fromX);
-     otherToX = image.getWidth() - 1;
+   } else if (otherToX > image.getWidth()) {
+     scale = (float) (image.getWidth() - fromX) / (float) (otherToX - fromX);
+     otherToX = image.getWidth();
    }
    int otherToY = (int) (fromY - (toY - fromY) * scale);
 
@@ -276,14 +274,14 @@ public class Detector {
    if (otherToY < 0) {
      scale = (float) fromY / (float) (fromY - otherToY);
      otherToY = 0;
-   } else if (otherToY >= image.getHeight()) {
-     scale = (float) (image.getHeight() - 1 - fromY) / (float) (otherToY - fromY);
-     otherToY = image.getHeight() - 1;
+   } else if (otherToY > image.getHeight()) {
+     scale = (float) (image.getHeight() - fromY) / (float) (otherToY - fromY);
+     otherToY = image.getHeight();
    }
    otherToX = (int) (fromX + (otherToX - fromX) * scale);
 
    result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
-   return result - 1.0f; // -1 because we counted the middle pixel twice
+   return result;
  }
 
   /**
@@ -330,6 +328,9 @@ public class Detector {
       if (state == 3) { // Found black, white, black, and stumbled back onto white; done
         int diffX = x - fromX;
         int diffY = y - fromY;
+        if (xstep < 0) {
+            diffX++;
+        }
         return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
       }
       error += dy;
@@ -373,6 +374,9 @@ public class Detector {
 
     int alignmentAreaTopY = Math.max(0, estAlignmentY - allowance);
     int alignmentAreaBottomY = Math.min(image.getHeight() - 1, estAlignmentY + allowance);
+    if (alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize * 3) {
+      throw NotFoundException.getNotFoundInstance();
+    }
 
     AlignmentPatternFinder alignmentFinder =
         new AlignmentPatternFinder(