--- /dev/null
+/*\r
+* Copyright 2007 ZXing authors\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+using DecodeHintType = com.google.zxing.DecodeHintType;\r
+using ReaderException = com.google.zxing.ReaderException;\r
+using ResultPoint = com.google.zxing.ResultPoint;\r
+using ResultPointCallback = com.google.zxing.ResultPointCallback;\r
+using Collections = com.google.zxing.common.Collections;\r
+using Comparator = com.google.zxing.common.Comparator;\r
+using BitMatrix = com.google.zxing.common.BitMatrix;\r
+namespace com.google.zxing.qrcode.detector\r
+{\r
+ \r
+ /// <summary> <p>This class attempts to find finder patterns in a QR Code. Finder patterns are the square\r
+ /// markers at three corners of a QR Code.</p>\r
+ /// \r
+ /// <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.\r
+ /// \r
+ /// </summary>\r
+ /// <author> Sean Owen\r
+ /// </author>\r
+ /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+ /// </author>\r
+ public class FinderPatternFinder\r
+ {\r
+ virtual protected internal BitMatrix Image\r
+ {\r
+ get\r
+ {\r
+ return image;\r
+ }\r
+ \r
+ }\r
+ virtual protected internal System.Collections.ArrayList PossibleCenters\r
+ {\r
+ get\r
+ {\r
+ return possibleCenters;\r
+ }\r
+ \r
+ }\r
+ private int[] CrossCheckStateCount\r
+ {\r
+ get\r
+ {\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
+ \r
+ private const int CENTER_QUORUM = 2;\r
+ protected internal const int MIN_SKIP = 3; // 1 pixel/module times 3 modules/center\r
+ protected internal const int MAX_MODULES = 57; // support up to version 10 for mobile clients\r
+ private const int INTEGER_MATH_SHIFT = 8;\r
+ \r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'image '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private BitMatrix image;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'possibleCenters '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private System.Collections.ArrayList possibleCenters;\r
+ private bool hasSkipped;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'crossCheckStateCount '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private int[] crossCheckStateCount;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'resultPointCallback '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private ResultPointCallback resultPointCallback;\r
+ \r
+ /// <summary> <p>Creates a finder that will search the image for three finder patterns.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="image">image to search\r
+ /// </param>\r
+ public FinderPatternFinder(BitMatrix image):this(image, null)\r
+ {\r
+ }\r
+ \r
+ public FinderPatternFinder(BitMatrix image, ResultPointCallback resultPointCallback)\r
+ {\r
+ this.image = image;\r
+ this.possibleCenters = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));\r
+ this.crossCheckStateCount = new int[5];\r
+ this.resultPointCallback = resultPointCallback;\r
+ }\r
+ \r
+ internal virtual FinderPatternInfo find(System.Collections.Hashtable hints)\r
+ {\r
+ bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER);\r
+ int maxI = image.Height;\r
+ int maxJ = image.Width;\r
+ // We are looking for black/white/black/white/black modules in\r
+ // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far\r
+ \r
+ // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the\r
+ // image, and then account for the center being 3 modules in size. This gives the smallest\r
+ // number of pixels the center could be, so skip this often. When trying harder, look for all\r
+ // QR versions regardless of how dense they are.\r
+ int iSkip = (3 * maxI) / (4 * MAX_MODULES);\r
+ if (iSkip < MIN_SKIP || tryHarder)\r
+ {\r
+ iSkip = MIN_SKIP;\r
+ }\r
+ \r
+ bool done = false;\r
+ int[] stateCount = new int[5];\r
+ for (int i = iSkip - 1; i < maxI && !done; i += iSkip)\r
+ {\r
+ // Get a row of black/white values\r
+ stateCount[0] = 0;\r
+ stateCount[1] = 0;\r
+ stateCount[2] = 0;\r
+ stateCount[3] = 0;\r
+ stateCount[4] = 0;\r
+ int currentState = 0;\r
+ for (int j = 0; j < maxJ; j++)\r
+ {\r
+ if (image.get_Renamed(j, i))\r
+ {\r
+ // Black pixel\r
+ if ((currentState & 1) == 1)\r
+ {\r
+ // Counting white pixels\r
+ currentState++;\r
+ }\r
+ stateCount[currentState]++;\r
+ }\r
+ else\r
+ {\r
+ // White pixel\r
+ if ((currentState & 1) == 0)\r
+ {\r
+ // Counting black pixels\r
+ if (currentState == 4)\r
+ {\r
+ // A winner?\r
+ if (foundPatternCross(stateCount))\r
+ {\r
+ // Yes\r
+ bool confirmed = handlePossibleCenter(stateCount, i, j);\r
+ if (confirmed)\r
+ {\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
+ {\r
+ done = haveMultiplyConfirmedCenters();\r
+ }\r
+ else\r
+ {\r
+ int rowSkip = findRowSkip();\r
+ if (rowSkip > stateCount[2])\r
+ {\r
+ // Skip rows between row of lower confirmed center\r
+ // and top of presumed third confirmed center\r
+ // but back up a bit to get a full chance of detecting\r
+ // it, entire width of center of finder pattern\r
+ \r
+ // Skip by rowSkip, but back off by stateCount[2] (size of last center\r
+ // of pattern we saw) to be conservative, and also back off by iSkip which\r
+ // is about to be re-added\r
+ i += rowSkip - stateCount[2] - iSkip;\r
+ j = maxJ - 1;\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // Advance to next black pixel\r
+ do \r
+ {\r
+ j++;\r
+ }\r
+ while (j < maxJ && !image.get_Renamed(j, i));\r
+ j--; // back up to that last white pixel\r
+ }\r
+ // Clear state to start looking again\r
+ currentState = 0;\r
+ stateCount[0] = 0;\r
+ stateCount[1] = 0;\r
+ stateCount[2] = 0;\r
+ stateCount[3] = 0;\r
+ stateCount[4] = 0;\r
+ }\r
+ else\r
+ {\r
+ // No, shift counts back by two\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
+ }\r
+ }\r
+ else\r
+ {\r
+ stateCount[++currentState]++;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // Counting white pixels\r
+ stateCount[currentState]++;\r
+ }\r
+ }\r
+ }\r
+ if (foundPatternCross(stateCount))\r
+ {\r
+ bool confirmed = handlePossibleCenter(stateCount, i, maxJ);\r
+ if (confirmed)\r
+ {\r
+ iSkip = stateCount[0];\r
+ if (hasSkipped)\r
+ {\r
+ // Found a third one\r
+ done = haveMultiplyConfirmedCenters();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ FinderPattern[] patternInfo = selectBestPatterns();\r
+ ResultPoint.orderBestPatterns(patternInfo);\r
+ \r
+ return new FinderPatternInfo(patternInfo);\r
+ }\r
+ \r
+ /// <summary> Given a count of black/white/black/white/black pixels just seen and an end position,\r
+ /// figures the location of the center of this run.\r
+ /// </summary>\r
+ private static float centerFromEnd(int[] stateCount, int end)\r
+ {\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ return (float) (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0f;\r
+ }\r
+ \r
+ /// <param name="stateCount">count of black/white/black/white/black pixels just read\r
+ /// </param>\r
+ /// <returns> true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios\r
+ /// used by finder patterns to be considered a match\r
+ /// </returns>\r
+ protected internal static bool foundPatternCross(int[] stateCount)\r
+ {\r
+ int totalModuleSize = 0;\r
+ for (int i = 0; i < 5; i++)\r
+ {\r
+ int count = stateCount[i];\r
+ if (count == 0)\r
+ {\r
+ return false;\r
+ }\r
+ totalModuleSize += count;\r
+ }\r
+ if (totalModuleSize < 7)\r
+ {\r
+ return false;\r
+ }\r
+ int moduleSize = (totalModuleSize << INTEGER_MATH_SHIFT) / 7;\r
+ int maxVariance = moduleSize / 2;\r
+ // Allow less than 50% variance from 1-1-3-1-1 proportions\r
+ return System.Math.Abs(moduleSize - (stateCount[0] << INTEGER_MATH_SHIFT)) < maxVariance && System.Math.Abs(moduleSize - (stateCount[1] << INTEGER_MATH_SHIFT)) < maxVariance && System.Math.Abs(3 * moduleSize - (stateCount[2] << INTEGER_MATH_SHIFT)) < 3 * maxVariance && System.Math.Abs(moduleSize - (stateCount[3] << INTEGER_MATH_SHIFT)) < maxVariance && System.Math.Abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance;\r
+ }\r
+ \r
+ /// <summary> <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
+ /// finder pattern to see if the same proportion is detected.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="startI">row where a finder pattern was detected\r
+ /// </param>\r
+ /// <param name="centerJ">center of the section that appears to cross a finder pattern\r
+ /// </param>\r
+ /// <param name="maxCount">maximum reasonable number of modules that should be\r
+ /// observed in any reading state, based on the results of the horizontal scan\r
+ /// </param>\r
+ /// <returns> vertical center of finder pattern, or {@link Float#NaN} if not found\r
+ /// </returns>\r
+ private float crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal)\r
+ {\r
+ BitMatrix image = this.image;\r
+ \r
+ int maxI = image.Height;\r
+ int[] stateCount = CrossCheckStateCount;\r
+ \r
+ // Start counting up from center\r
+ int i = startI;\r
+ while (i >= 0 && image.get_Renamed(centerJ, i))\r
+ {\r
+ stateCount[2]++;\r
+ i--;\r
+ }\r
+ if (i < 0)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (i >= 0 && !image.get_Renamed(centerJ, i) && stateCount[1] <= maxCount)\r
+ {\r
+ stateCount[1]++;\r
+ i--;\r
+ }\r
+ // If already too many modules in this state or ran off the edge:\r
+ if (i < 0 || stateCount[1] > maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (i >= 0 && image.get_Renamed(centerJ, i) && stateCount[0] <= maxCount)\r
+ {\r
+ stateCount[0]++;\r
+ i--;\r
+ }\r
+ if (stateCount[0] > maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \r
+ // Now also count down from center\r
+ i = startI + 1;\r
+ while (i < maxI && image.get_Renamed(centerJ, i))\r
+ {\r
+ stateCount[2]++;\r
+ i++;\r
+ }\r
+ if (i == maxI)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (i < maxI && !image.get_Renamed(centerJ, i) && stateCount[3] < maxCount)\r
+ {\r
+ stateCount[3]++;\r
+ i++;\r
+ }\r
+ if (i == maxI || stateCount[3] >= maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (i < maxI && image.get_Renamed(centerJ, i) && stateCount[4] < maxCount)\r
+ {\r
+ stateCount[4]++;\r
+ i++;\r
+ }\r
+ if (stateCount[4] >= maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \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] + stateCount[4];\r
+ if (5 * System.Math.Abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \r
+ return foundPatternCross(stateCount)?centerFromEnd(stateCount, i):System.Single.NaN;\r
+ }\r
+ \r
+ /// <summary> <p>Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical,\r
+ /// except it reads horizontally instead of vertically. This is used to cross-cross\r
+ /// check a vertical cross check and locate the real center of the alignment pattern.</p>\r
+ /// </summary>\r
+ private float crossCheckHorizontal(int startJ, int centerI, int maxCount, int originalStateCountTotal)\r
+ {\r
+ BitMatrix image = this.image;\r
+ \r
+ int maxJ = image.Width;\r
+ int[] stateCount = CrossCheckStateCount;\r
+ \r
+ int j = startJ;\r
+ while (j >= 0 && image.get_Renamed(j, centerI))\r
+ {\r
+ stateCount[2]++;\r
+ j--;\r
+ }\r
+ if (j < 0)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (j >= 0 && !image.get_Renamed(j, centerI) && stateCount[1] <= maxCount)\r
+ {\r
+ stateCount[1]++;\r
+ j--;\r
+ }\r
+ if (j < 0 || stateCount[1] > maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (j >= 0 && image.get_Renamed(j, centerI) && stateCount[0] <= maxCount)\r
+ {\r
+ stateCount[0]++;\r
+ j--;\r
+ }\r
+ if (stateCount[0] > maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \r
+ j = startJ + 1;\r
+ while (j < maxJ && image.get_Renamed(j, centerI))\r
+ {\r
+ stateCount[2]++;\r
+ j++;\r
+ }\r
+ if (j == maxJ)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (j < maxJ && !image.get_Renamed(j, centerI) && stateCount[3] < maxCount)\r
+ {\r
+ stateCount[3]++;\r
+ j++;\r
+ }\r
+ if (j == maxJ || stateCount[3] >= maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (j < maxJ && image.get_Renamed(j, centerI) && stateCount[4] < maxCount)\r
+ {\r
+ stateCount[4]++;\r
+ j++;\r
+ }\r
+ if (stateCount[4] >= maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \r
+ // If we found a finder-pattern-like section, but its size is significantly different than\r
+ // the original, assume it's a false positive\r
+ int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];\r
+ if (5 * System.Math.Abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \r
+ return foundPatternCross(stateCount)?centerFromEnd(stateCount, j):System.Single.NaN;\r
+ }\r
+ \r
+ /// <summary> <p>This is called when a horizontal scan finds a possible alignment pattern. It will\r
+ /// cross check with a vertical scan, and if successful, will, ah, cross-cross-check\r
+ /// with another horizontal scan. This is needed primarily to locate the real horizontal\r
+ /// center of the pattern in cases of extreme skew.</p>\r
+ /// \r
+ /// <p>If that succeeds the finder pattern location is added to a list that tracks\r
+ /// the number of times each location has been nearly-matched as a finder pattern.\r
+ /// Each additional find is more evidence that the location is in fact a finder\r
+ /// pattern center\r
+ /// \r
+ /// </summary>\r
+ /// <param name="stateCount">reading state module counts from horizontal scan\r
+ /// </param>\r
+ /// <param name="i">row where finder pattern may be found\r
+ /// </param>\r
+ /// <param name="j">end of possible finder pattern in row\r
+ /// </param>\r
+ /// <returns> true if a finder pattern candidate was found this time\r
+ /// </returns>\r
+ protected internal virtual bool handlePossibleCenter(int[] stateCount, int i, int j)\r
+ {\r
+ int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];\r
+ float centerJ = centerFromEnd(stateCount, j);\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ float centerI = crossCheckVertical(i, (int) centerJ, stateCount[2], stateCountTotal);\r
+ if (!System.Single.IsNaN(centerI))\r
+ {\r
+ // Re-cross check\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ centerJ = crossCheckHorizontal((int) centerJ, (int) centerI, stateCount[2], stateCountTotal);\r
+ if (!System.Single.IsNaN(centerJ))\r
+ {\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ float estimatedModuleSize = (float) stateCountTotal / 7.0f;\r
+ bool found = false;\r
+ int max = possibleCenters.Count;\r
+ for (int index = 0; index < max; index++)\r
+ {\r
+ FinderPattern center = (FinderPattern) possibleCenters[index];\r
+ // Look for about the same center and module size:\r
+ if (center.aboutEquals(estimatedModuleSize, centerI, centerJ))\r
+ {\r
+ center.incrementCount();\r
+ found = true;\r
+ break;\r
+ }\r
+ }\r
+ if (!found)\r
+ {\r
+ ResultPoint point = new FinderPattern(centerJ, centerI, estimatedModuleSize);\r
+ possibleCenters.Add(point);\r
+ if (resultPointCallback != null)\r
+ {\r
+ resultPointCallback.foundPossibleResultPoint(point);\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /// <returns> number of rows we could safely skip during scanning, based on the first\r
+ /// two finder patterns that have been located. In some cases their position will\r
+ /// allow us to infer that the third pattern must lie below a certain point farther\r
+ /// down in the image.\r
+ /// </returns>\r
+ private int findRowSkip()\r
+ {\r
+ int max = possibleCenters.Count;\r
+ if (max <= 1)\r
+ {\r
+ return 0;\r
+ }\r
+ FinderPattern firstConfirmedCenter = null;\r
+ for (int i = 0; i < max; i++)\r
+ {\r
+ FinderPattern center = (FinderPattern) possibleCenters[i];\r
+ if (center.Count >= CENTER_QUORUM)\r
+ {\r
+ if (firstConfirmedCenter == null)\r
+ {\r
+ firstConfirmedCenter = center;\r
+ }\r
+ else\r
+ {\r
+ // We have two confirmed centers\r
+ // 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 last.\r
+ hasSkipped = true;\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ return (int) (System.Math.Abs(firstConfirmedCenter.X - center.X) - System.Math.Abs(firstConfirmedCenter.Y - center.Y)) / 2;\r
+ }\r
+ }\r
+ }\r
+ return 0;\r
+ }\r
+ \r
+ /// <returns> true iff we have found at least 3 finder patterns that have been detected\r
+ /// at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the\r
+ /// candidates is "pretty similar"\r
+ /// </returns>\r
+ private bool haveMultiplyConfirmedCenters()\r
+ {\r
+ int confirmedCount = 0;\r
+ float totalModuleSize = 0.0f;\r
+ int max = possibleCenters.Count;\r
+ for (int i = 0; i < max; i++)\r
+ {\r
+ FinderPattern pattern = (FinderPattern) possibleCenters[i];\r
+ if (pattern.Count >= CENTER_QUORUM)\r
+ {\r
+ confirmedCount++;\r
+ totalModuleSize += pattern.EstimatedModuleSize;\r
+ }\r
+ }\r
+ if (confirmedCount < 3)\r
+ {\r
+ return false;\r
+ }\r
+ // OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive"\r
+ // and that we need to keep looking. We detect this by asking if the estimated module sizes\r
+ // vary too much. We arbitrarily say that when the total deviation from average exceeds\r
+ // 5% of the total module size estimates, it's too much.\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ float average = totalModuleSize / (float) max;\r
+ float totalDeviation = 0.0f;\r
+ for (int i = 0; i < max; i++)\r
+ {\r
+ FinderPattern pattern = (FinderPattern) possibleCenters[i];\r
+ totalDeviation += System.Math.Abs(pattern.EstimatedModuleSize - average);\r
+ }\r
+ return totalDeviation <= 0.05f * totalModuleSize;\r
+ }\r
+ \r
+ /// <returns> 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
+ /// </returns>\r
+ /// <throws> ReaderException if 3 such finder patterns do not exist </throws>\r
+ private FinderPattern[] selectBestPatterns()\r
+ {\r
+ \r
+ int startSize = possibleCenters.Count;\r
+ if (startSize < 3)\r
+ {\r
+ // Couldn't find enough finder patterns\r
+ throw ReaderException.Instance;\r
+ }\r
+ \r
+ // Filter outlier possibilities whose module size is too different\r
+ if (startSize > 3)\r
+ {\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
+ for (int i = 0; i < startSize; i++)\r
+ {\r
+ totalModuleSize += ((FinderPattern) possibleCenters[i]).EstimatedModuleSize;\r
+ }\r
+ //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+ float average = totalModuleSize / (float) startSize;\r
+ for (int i = 0; i < possibleCenters.Count && possibleCenters.Count > 3; i++)\r
+ {\r
+ FinderPattern pattern = (FinderPattern) possibleCenters[i];\r
+ if (System.Math.Abs(pattern.EstimatedModuleSize - average) > 0.2f * average)\r
+ {\r
+ possibleCenters.RemoveAt(i);\r
+ i--;\r
+ }\r
+ }\r
+ }\r
+ \r
+ if (possibleCenters.Count > 3)\r
+ {\r
+ // Throw away all but those first size candidate points we found.\r
+ Collections.insertionSort(possibleCenters, new CenterComparator());\r
+ SupportClass.SetCapacity(possibleCenters, 3);\r
+ }\r
+ \r
+ return new FinderPattern[]{(FinderPattern) possibleCenters[0], (FinderPattern) possibleCenters[1], (FinderPattern) possibleCenters[2]};\r
+ }\r
+ \r
+ /// <summary> <p>Orders by {@link FinderPattern#getCount()}, descending.</p></summary>\r
+ private class CenterComparator : Comparator\r
+ {\r
+ public virtual int compare(System.Object center1, System.Object center2)\r
+ {\r
+ return ((FinderPattern) center2).Count - ((FinderPattern) center1).Count;\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file