Various code tweaks and refactorings suggested by IntelliJ
[zxing.git] / core / src / com / google / zxing / qrcode / detector / AlignmentPatternFinder.java
index e68d3a1..f9f531c 100644 (file)
@@ -94,14 +94,14 @@ final class AlignmentPatternFinder {
       stateCount[0] = 0;\r
       stateCount[1] = 0;\r
       stateCount[2] = 0;\r
-      int currentState = 0;\r
       int j = startX;\r
       // Burn off leading white pixels before anything else; if we start in the middle of\r
       // a white run, it doesn't make sense to count its length, since we don't know if the\r
       // white run continued to the left of the start point\r
-      while (!luminanceRow.get(j - startX) && j < maxJ) {\r
+      while (j < maxJ && !luminanceRow.get(j - startX)) {\r
         j++;\r
       }\r
+      int currentState = 0;\r
       while (j < maxJ) {\r
         if (luminanceRow.get(j - startX)) {\r
           // Black pixel\r
@@ -160,11 +160,11 @@ final class AlignmentPatternFinder {
   /**\r
    * @param stateCount count of black/white/black pixels just read\r
    * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios\r
-   *  used by alignment patterns to be considered a match\r
+   *         used by alignment patterns to be considered a match\r
    */\r
   private boolean foundPatternCross(int[] stateCount) {\r
     float moduleSize = this.moduleSize;\r
-    float maxVariance = moduleSize / 2.5f;\r
+    float maxVariance = moduleSize / 2.0f;\r
     for (int i = 0; i < 3; i++) {\r
       if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) {\r
         return false;\r
@@ -181,10 +181,10 @@ final class AlignmentPatternFinder {
    * @param startI row where an alignment pattern was detected\r
    * @param centerJ center of the section that appears to cross an alignment pattern\r
    * @param maxCount maximum reasonable number of modules that should be\r
-   *  observed in any reading state, based on the results of the horizontal scan\r
+   * observed in any reading state, based on the results of the horizontal scan\r
    * @return vertical center of alignment pattern, or {@link Float#NaN} if not found\r
    */\r
-  private float crossCheckVertical(int startI, int centerJ, int maxCount) {\r
+  private float crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal) {\r
     MonochromeBitmapSource image = this.image;\r
 \r
     int maxI = image.getHeight();\r
@@ -225,6 +225,11 @@ final class AlignmentPatternFinder {
       return Float.NaN;\r
     }\r
 \r
+    int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];\r
+    if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) {\r
+      return Float.NaN;\r
+    }\r
+\r
     return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : Float.NaN;\r
   }\r
 \r
@@ -240,8 +245,9 @@ final class AlignmentPatternFinder {
    * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not\r
    */\r
   private AlignmentPattern handlePossibleCenter(int[] stateCount, int i, int j) {\r
+    int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];\r
     float centerJ = centerFromEnd(stateCount, j);\r
-    float centerI = crossCheckVertical(i, (int) centerJ, 2 * stateCount[1]);\r
+    float centerI = crossCheckVertical(i, (int) centerJ, 2 * stateCount[1], stateCountTotal);\r
     if (!Float.isNaN(centerI)) {\r
       float estimatedModuleSize = (float) (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f;\r
       int max = possibleCenters.size();\r