Improve correctness of black-run logic in case where patterns are very close to edge
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 18 Dec 2007 21:45:44 +0000 (21:45 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 18 Dec 2007 21:45:44 +0000 (21:45 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@124 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/qrcode/detector/Detector.java

index 49c255c..3f20bdb 100644 (file)
@@ -172,8 +172,23 @@ public final class Detector {
    * of another point (another finder pattern center), and in the opposite direction too.</p>
    */
   private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) {
+
     float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
-    result += sizeOfBlackWhiteBlackRun(fromX, fromY, fromX - (toX - fromX), fromY - (toY - fromY));
+
+    // Now count other way -- don't run off image though of course
+    int otherToX = fromX - (toX - fromX);
+    if (otherToX < 0) {
+      otherToX = 0;
+    } else if (otherToX >= image.getWidth()) {
+      otherToX = image.getWidth();
+    }
+    int otherToY = fromY - (toY - fromY);
+    if (otherToY < 0) {
+      otherToY = 0;
+    } else if (otherToY >= image.getHeight()) {
+      otherToY = image.getHeight();
+    }
+    result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
     return result - 1.0f; // -1 because we counted the middle pixel twice
   }
 
@@ -229,8 +244,9 @@ public final class Detector {
         error -= dx;
       }
     }
-    // Hmm, couldn't find all of what we wanted -- don't know
-    return Float.NaN;
+    int diffX = toX - fromX;
+    int diffY = toY - fromY;
+    return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
   }
 
   /**