Tightened a condition to reduce accidentally detecting the alignment pattern as a...
[zxing.git] / core / src / com / google / zxing / qrcode / detector / FinderPatternFinder.java
index 160fd75..63592bf 100755 (executable)
@@ -19,10 +19,10 @@ package com.google.zxing.qrcode.detector;
 import com.google.zxing.DecodeHintType;\r
 import com.google.zxing.MonochromeBitmapSource;\r
 import com.google.zxing.ReaderException;\r
-import com.google.zxing.ResultPoint;\r
 import com.google.zxing.common.BitArray;\r
 import com.google.zxing.common.Collections;\r
 import com.google.zxing.common.Comparator;\r
+import com.google.zxing.common.GenericResultPoint;\r
 \r
 import java.util.Hashtable;\r
 import java.util.Vector;\r
@@ -33,7 +33,7 @@ import java.util.Vector;
  *\r
  * <p>This class is not thread-safe and should not be reused.</p>\r
  *\r
- * @author srowen@google.com (Sean Owen)\r
+ * @author Sean Owen\r
  */\r
 final class FinderPatternFinder {\r
 \r
@@ -45,6 +45,7 @@ final class FinderPatternFinder {
   private final MonochromeBitmapSource image;\r
   private final Vector possibleCenters;\r
   private boolean hasSkipped;\r
+  private final int[] crossCheckStateCount;\r
 \r
   /**\r
    * <p>Creates a finder that will search the image for three finder patterns.</p>\r
@@ -54,6 +55,7 @@ final class FinderPatternFinder {
   FinderPatternFinder(MonochromeBitmapSource image) {\r
     this.image = image;\r
     this.possibleCenters = new Vector();\r
+    this.crossCheckStateCount = new int[5];\r
   }\r
 \r
   FinderPatternInfo find(Hashtable hints) throws ReaderException {\r
@@ -74,9 +76,10 @@ final class FinderPatternFinder {
 \r
     boolean done = false;\r
     int[] stateCount = new int[5];\r
+    BitArray blackRow = new BitArray(maxJ);\r
     for (int i = iSkip - 1; i < maxI && !done; i += iSkip) {\r
       // Get a row of black/white values\r
-      BitArray blackRow = image.getBlackRow(i, null, 0, maxJ);\r
+      blackRow = image.getBlackRow(i, blackRow, 0, maxJ);\r
       stateCount[0] = 0;\r
       stateCount[1] = 0;\r
       stateCount[2] = 0;\r
@@ -96,7 +99,9 @@ final class FinderPatternFinder {
               if (foundPatternCross(stateCount)) { // Yes\r
                 boolean confirmed = handlePossibleCenter(stateCount, i, j);\r
                 if (confirmed) {\r
-                  iSkip = 1; // Go back to examining each line\r
+                  // Start examining every other line. Checking each line turned out to be too\r
+                  // expensive and didn't improve performance.\r
+                  iSkip = 2;\r
                   if (hasSkipped) {\r
                     done = haveMulitplyConfirmedCenters();\r
                   } else {\r
@@ -157,7 +162,7 @@ final class FinderPatternFinder {
     }\r
 \r
     FinderPattern[] patternInfo = selectBestPatterns();\r
-    patternInfo = orderBestPatterns(patternInfo);\r
+    GenericResultPoint.orderBestPatterns(patternInfo);\r
 \r
     return new FinderPatternInfo(patternInfo);\r
   }\r
@@ -197,6 +202,15 @@ final class FinderPatternFinder {
         Math.abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance;\r
   }\r
 \r
+  private int[] getCrossCheckStateCount() {\r
+    crossCheckStateCount[0] = 0;\r
+    crossCheckStateCount[1] = 0;\r
+    crossCheckStateCount[2] = 0;\r
+    crossCheckStateCount[3] = 0;\r
+    crossCheckStateCount[4] = 0;\r
+    return crossCheckStateCount;\r
+  }\r
+\r
   /**\r
    * <p>After a horizontal scan finds a potential finder pattern, this method\r
    * "cross-checks" by scanning down vertically through the center of the possible\r
@@ -212,7 +226,7 @@ final class FinderPatternFinder {
     MonochromeBitmapSource image = this.image;\r
 \r
     int maxI = image.getHeight();\r
-    int[] stateCount = new int[5];\r
+    int[] stateCount = getCrossCheckStateCount();\r
 \r
     // Start counting up from center\r
     int i = startI;\r
@@ -282,7 +296,7 @@ final class FinderPatternFinder {
     MonochromeBitmapSource image = this.image;\r
 \r
     int maxJ = image.getWidth();\r
-    int[] stateCount = new int[5];\r
+    int[] stateCount = getCrossCheckStateCount();\r
 \r
     int j = startJ;\r
     while (j >= 0 && image.isBlack(j, centerI)) {\r
@@ -409,10 +423,10 @@ final class FinderPatternFinder {
           // How far down can we skip before resuming looking for the next\r
           // pattern? In the worst case, only the difference between the\r
           // difference in the x / y coordinates of the two centers.\r
-          // This is the case where you find top left first. Draw it out.\r
+          // This is the case where you find top left last.\r
           hasSkipped = true;\r
-          return (int) Math.abs(Math.abs(firstConfirmedCenter.getX() - center.getX()) -\r
-              Math.abs(firstConfirmedCenter.getY() - center.getY()));\r
+          return (int) (Math.abs(firstConfirmedCenter.getX() - center.getX()) -\r
+              Math.abs(firstConfirmedCenter.getY() - center.getY())) / 2;\r
         }\r
       }\r
     }\r
@@ -448,7 +462,7 @@ final class FinderPatternFinder {
       FinderPattern pattern = (FinderPattern) possibleCenters.elementAt(i);\r
       totalDeviation += Math.abs(pattern.getEstimatedModuleSize() - average);\r
     }\r
-    return totalDeviation <= 0.15f * totalModuleSize;\r
+    return totalDeviation <= 0.05f * totalModuleSize;\r
   }\r
 \r
   /**\r
@@ -470,31 +484,22 @@ final class FinderPatternFinder {
 \r
     if (size < 3) {\r
       // Couldn't find enough finder patterns\r
-      throw new ReaderException("Could not find three finder patterns");\r
-    }\r
-\r
-    if (size == 3) {\r
-      // Found just enough -- hope these are good!\r
-      return new FinderPattern[]{\r
-          (FinderPattern) possibleCenters.elementAt(0),\r
-          (FinderPattern) possibleCenters.elementAt(1),\r
-          (FinderPattern) possibleCenters.elementAt(2)\r
-      };\r
+      throw ReaderException.getInstance();\r
     }\r
 \r
-    possibleCenters.setSize(size);\r
-\r
-    // Hmm, multiple found. We need to pick the best three. Find the most\r
-    // popular ones whose module size is nearest the average\r
-\r
-    float averageModuleSize = 0.0f;\r
-    for (int i = 0; i < size; i++) {\r
-      averageModuleSize += ((FinderPattern) possibleCenters.elementAt(i)).getEstimatedModuleSize();\r
+    if (size > 3) {\r
+      // Throw away all but those first size candidate points we found.\r
+      possibleCenters.setSize(size);\r
+      //  We need to pick the best three. Find the most\r
+      // popular ones whose module size is nearest the average\r
+      float averageModuleSize = 0.0f;\r
+      for (int i = 0; i < size; i++) {\r
+        averageModuleSize += ((FinderPattern) possibleCenters.elementAt(i)).getEstimatedModuleSize();\r
+      }\r
+      averageModuleSize /= (float) size;\r
+      // We don't have java.util.Collections in J2ME\r
+      Collections.insertionSort(possibleCenters, new ClosestToAverageComparator(averageModuleSize));\r
     }\r
-    averageModuleSize /= (float) size;\r
-\r
-    // We don't have java.util.Collections in J2ME\r
-    Collections.insertionSort(possibleCenters, new ClosestToAverageComparator(averageModuleSize));\r
 \r
     return new FinderPattern[]{\r
         (FinderPattern) possibleCenters.elementAt(0),\r
@@ -503,63 +508,6 @@ final class FinderPatternFinder {
     };\r
   }\r
 \r
-  /**\r
-   * <p>Having found three "best" finder patterns we need to decide which is the top-left, top-right,\r
-   * bottom-left. We assume that the one closest to the other two is the top-left one; this is not\r
-   * strictly true (imagine extreme perspective distortion) but for the moment is a serviceable assumption.\r
-   * Lastly we sort top-right from bottom-left by figuring out orientation from vector cross products.</p>\r
-   *\r
-   * @param patterns three best {@link FinderPattern}s\r
-   * @return same {@link FinderPattern}s ordered bottom-left, top-left, top-right\r
-   */\r
-  private static FinderPattern[] orderBestPatterns(FinderPattern[] patterns) {\r
-\r
-    // Find distances between pattern centers\r
-    float abDistance = distance(patterns[0], patterns[1]);\r
-    float bcDistance = distance(patterns[1], patterns[2]);\r
-    float acDistance = distance(patterns[0], patterns[2]);\r
-\r
-    FinderPattern topLeft;\r
-    FinderPattern topRight;\r
-    FinderPattern bottomLeft;\r
-    // Assume one closest to other two is top left;\r
-    // topRight and bottomLeft will just be guesses below at first\r
-    if (bcDistance >= abDistance && bcDistance >= acDistance) {\r
-      topLeft = patterns[0];\r
-      topRight = patterns[1];\r
-      bottomLeft = patterns[2];\r
-    } else if (acDistance >= bcDistance && acDistance >= abDistance) {\r
-      topLeft = patterns[1];\r
-      topRight = patterns[0];\r
-      bottomLeft = patterns[2];\r
-    } else {\r
-      topLeft = patterns[2];\r
-      topRight = patterns[0];\r
-      bottomLeft = patterns[1];\r
-    }\r
-\r
-    // Use cross product to figure out which of other1/2 is the bottom left\r
-    // pattern. The vector "top-left -> bottom-left" x "top-left -> top-right"\r
-    // should yield a vector with positive z component\r
-    if ((bottomLeft.getY() - topLeft.getY()) * (topRight.getX() - topLeft.getX()) <\r
-        (bottomLeft.getX() - topLeft.getX()) * (topRight.getY() - topLeft.getY())) {\r
-      FinderPattern temp = topRight;\r
-      topRight = bottomLeft;\r
-      bottomLeft = temp;\r
-    }\r
-\r
-    return new FinderPattern[]{bottomLeft, topLeft, topRight};\r
-  }\r
-\r
-  /**\r
-   * @return distance between two points\r
-   */\r
-  static float distance(ResultPoint pattern1, ResultPoint pattern2) {\r
-    float xDiff = pattern1.getX() - pattern2.getX();\r
-    float yDiff = pattern1.getY() - pattern2.getY();\r
-    return (float) Math.sqrt((double) (xDiff * xDiff + yDiff * yDiff));\r
-  }\r
-\r
   /**\r
    * <p>Orders by {@link FinderPattern#getCount()}, descending.</p>\r
    */\r