Small style stuff
[zxing.git] / core / src / com / google / zxing / qrcode / detector / FinderPatternFinder.java
index 199b895..9968f00 100755 (executable)
 package com.google.zxing.qrcode.detector;\r
 \r
 import com.google.zxing.DecodeHintType;\r
-import com.google.zxing.ReaderException;\r
+import com.google.zxing.NotFoundException;\r
 import com.google.zxing.ResultPoint;\r
+import com.google.zxing.ResultPointCallback;\r
+import com.google.zxing.common.BitMatrix;\r
 import com.google.zxing.common.Collections;\r
 import com.google.zxing.common.Comparator;\r
-import com.google.zxing.common.BitMatrix;\r
 \r
 import java.util.Hashtable;\r
 import java.util.Vector;\r
@@ -45,6 +46,7 @@ public class FinderPatternFinder {
   private final Vector possibleCenters;\r
   private boolean hasSkipped;\r
   private final int[] crossCheckStateCount;\r
+  private final ResultPointCallback resultPointCallback;\r
 \r
   /**\r
    * <p>Creates a finder that will search the image for three finder patterns.</p>\r
@@ -52,9 +54,14 @@ public class FinderPatternFinder {
    * @param image image to search\r
    */\r
   public FinderPatternFinder(BitMatrix image) {\r
+    this(image, null);\r
+  }\r
+\r
+  public FinderPatternFinder(BitMatrix image, ResultPointCallback resultPointCallback) {\r
     this.image = image;\r
     this.possibleCenters = new Vector();\r
     this.crossCheckStateCount = new int[5];\r
+    this.resultPointCallback = resultPointCallback;\r
   }\r
 \r
   protected BitMatrix getImage() {\r
@@ -65,7 +72,7 @@ public class FinderPatternFinder {
     return possibleCenters;\r
   }\r
 \r
-  FinderPatternInfo find(Hashtable hints) throws ReaderException {\r
+  FinderPatternInfo find(Hashtable hints) throws NotFoundException {\r
     boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);\r
     int maxI = image.getHeight();\r
     int maxJ = image.getWidth();\r
@@ -125,11 +132,13 @@ public class FinderPatternFinder {
                     }\r
                   }\r
                 } else {\r
-                  // Advance to next black pixel\r
-                  do {\r
-                    j++;\r
-                  } while (j < maxJ && !image.get(j, i));\r
-                  j--; // back up to that last white pixel\r
+                  stateCount[0] = stateCount[2];\r
+                  stateCount[1] = stateCount[3];\r
+                  stateCount[2] = stateCount[4];\r
+                  stateCount[3] = 1;\r
+                  stateCount[4] = 0;\r
+                  currentState = 3;\r
+                  continue;\r
                 }\r
                 // Clear state to start looking again\r
                 currentState = 0;\r
@@ -283,11 +292,11 @@ public class FinderPatternFinder {
       return Float.NaN;\r
     }\r
 \r
-    // If we found a finder-pattern-like section, but its size is more than 20% different than\r
+    // If we found a finder-pattern-like section, but its size is more than 40% different than\r
     // the original, assume it's a false positive\r
     int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +\r
         stateCount[4];\r
-    if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) {\r
+    if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {\r
       return Float.NaN;\r
     }\r
 \r
@@ -379,7 +388,7 @@ public class FinderPatternFinder {
    * @param j end of possible finder pattern in row\r
    * @return true if a finder pattern candidate was found this time\r
    */\r
-  protected boolean handlePossibleCenter(int[] stateCount, int i, int j) throws ReaderException {\r
+  protected boolean handlePossibleCenter(int[] stateCount, int i, int j) {\r
     int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +\r
         stateCount[4];\r
     float centerJ = centerFromEnd(stateCount, j);\r
@@ -401,7 +410,11 @@ public class FinderPatternFinder {
           }\r
         }\r
         if (!found) {\r
-          possibleCenters.addElement(new FinderPattern(centerJ, centerI, estimatedModuleSize));\r
+          ResultPoint point = new FinderPattern(centerJ, centerI, estimatedModuleSize);\r
+          possibleCenters.addElement(point);\r
+          if (resultPointCallback != null) {\r
+            resultPointCallback.foundPossibleResultPoint(point);\r
+          }\r
         }\r
         return true;\r
       }\r
@@ -477,28 +490,37 @@ public class FinderPatternFinder {
    * @return the 3 best {@link FinderPattern}s from our list of candidates. The "best" are\r
    *         those that have been detected at least {@link #CENTER_QUORUM} times, and whose module\r
    *         size differs from the average among those patterns the least\r
-   * @throws ReaderException if 3 such finder patterns do not exist\r
+   * @throws NotFoundException if 3 such finder patterns do not exist\r
    */\r
-  private FinderPattern[] selectBestPatterns() throws ReaderException {\r
+  private FinderPattern[] selectBestPatterns() throws NotFoundException {\r
 \r
     int startSize = possibleCenters.size();\r
     if (startSize < 3) {\r
       // Couldn't find enough finder patterns\r
-      throw ReaderException.getInstance();\r
+      throw NotFoundException.getNotFoundInstance();\r
     }\r
 \r
     // Filter outlier possibilities whose module size is too different\r
     if (startSize > 3) {\r
       // But we can only afford to do so if we have at least 4 possibilities to choose from\r
       float totalModuleSize = 0.0f;\r
+      float square = 0.0f;\r
       for (int i = 0; i < startSize; i++) {\r
-        totalModuleSize += ((FinderPattern) possibleCenters.get(i)).getEstimatedModuleSize();\r
+        float size = ((FinderPattern) possibleCenters.elementAt(i)).getEstimatedModuleSize();\r
+        totalModuleSize += size;\r
+        square += size * size;\r
       }\r
       float average = totalModuleSize / (float) startSize;\r
+      float stdDev = (float) Math.sqrt(square / startSize - average * average);\r
+\r
+      Collections.insertionSort(possibleCenters, new FurthestFromAverageComparator(average));\r
+\r
+      float limit = Math.max(0.2f * average, stdDev);\r
+\r
       for (int i = 0; i < possibleCenters.size() && possibleCenters.size() > 3; i++) {\r
-        FinderPattern pattern = (FinderPattern) possibleCenters.get(i);\r
-        if (Math.abs(pattern.getEstimatedModuleSize() - average) > 0.2f * average) {\r
-          possibleCenters.remove(i);\r
+        FinderPattern pattern = (FinderPattern) possibleCenters.elementAt(i);\r
+        if (Math.abs(pattern.getEstimatedModuleSize() - average) > limit) {\r
+          possibleCenters.removeElementAt(i);\r
           i--;\r
         }\r
       }\r
@@ -506,7 +528,16 @@ public class FinderPatternFinder {
 \r
     if (possibleCenters.size() > 3) {\r
       // Throw away all but those first size candidate points we found.\r
-      Collections.insertionSort(possibleCenters, new CenterComparator());      \r
+\r
+      float totalModuleSize = 0.0f;\r
+      for (int i = 0; i < possibleCenters.size(); i++) {\r
+        totalModuleSize += ((FinderPattern) possibleCenters.elementAt(i)).getEstimatedModuleSize();\r
+      }\r
+\r
+      float average = totalModuleSize / (float) possibleCenters.size();\r
+\r
+      Collections.insertionSort(possibleCenters, new CenterComparator(average));\r
+\r
       possibleCenters.setSize(3);\r
     }\r
 \r
@@ -517,12 +548,37 @@ public class FinderPatternFinder {
     };\r
   }\r
 \r
+  /**\r
+   * <p>Orders by furthest from average</p>\r
+   */\r
+  private static class FurthestFromAverageComparator implements Comparator {\r
+    private final float average;\r
+    private FurthestFromAverageComparator(float f) {\r
+      average = f;\r
+    }\r
+    public int compare(Object center1, Object center2) {\r
+      float dA = Math.abs(((FinderPattern) center2).getEstimatedModuleSize() - average);\r
+      float dB = Math.abs(((FinderPattern) center1).getEstimatedModuleSize() - average);\r
+      return dA < dB ? -1 : (dA == dB ? 0 : 1);\r
+    }\r
+  }\r
+\r
   /**\r
    * <p>Orders by {@link FinderPattern#getCount()}, descending.</p>\r
    */\r
   private static class CenterComparator implements Comparator {\r
+    private final float average;\r
+    private CenterComparator(float f) {\r
+      average = f;\r
+    }\r
     public int compare(Object center1, Object center2) {\r
-      return ((FinderPattern) center2).getCount() - ((FinderPattern) center1).getCount();\r
+      if (((FinderPattern) center2).getCount() == ((FinderPattern) center1).getCount()) {\r
+        float dA = Math.abs(((FinderPattern) center2).getEstimatedModuleSize() - average);\r
+        float dB = Math.abs(((FinderPattern) center1).getEstimatedModuleSize() - average);\r
+        return dA < dB ? 1 : (dA == dB ? 0 : -1);\r
+      } else {\r
+        return ((FinderPattern) center2).getCount() - ((FinderPattern) center1).getCount();\r
+      }\r
     }\r
   }\r
 \r