--- /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 ReaderException = com.google.zxing.ReaderException;\r
+using ResultPoint = com.google.zxing.ResultPoint;\r
+using ResultPointCallback = com.google.zxing.ResultPointCallback;\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 alignment patterns in a QR Code. Alignment patterns look like finder\r
+ /// patterns but are smaller and appear at regular intervals throughout the image.</p>\r
+ /// \r
+ /// <p>At the moment this only looks for the bottom-right alignment pattern.</p>\r
+ /// \r
+ /// <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,\r
+ /// pasted and stripped down here for maximum performance but does unfortunately duplicate\r
+ /// some 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
+ sealed class AlignmentPatternFinder\r
+ {\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
+ //UPGRADE_NOTE: Final was removed from the declaration of 'startX '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private int startX;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'startY '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private int startY;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'width '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private int width;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'height '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private int height;\r
+ //UPGRADE_NOTE: Final was removed from the declaration of 'moduleSize '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+ private float moduleSize;\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 look in a portion of the whole image.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="image">image to search\r
+ /// </param>\r
+ /// <param name="startX">left column from which to start searching\r
+ /// </param>\r
+ /// <param name="startY">top row from which to start searching\r
+ /// </param>\r
+ /// <param name="width">width of region to search\r
+ /// </param>\r
+ /// <param name="height">height of region to search\r
+ /// </param>\r
+ /// <param name="moduleSize">estimated module size so far\r
+ /// </param>\r
+ internal AlignmentPatternFinder(BitMatrix image, int startX, int startY, int width, int height, float moduleSize, ResultPointCallback resultPointCallback)\r
+ {\r
+ this.image = image;\r
+ this.possibleCenters = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(5));\r
+ this.startX = startX;\r
+ this.startY = startY;\r
+ this.width = width;\r
+ this.height = height;\r
+ this.moduleSize = moduleSize;\r
+ this.crossCheckStateCount = new int[3];\r
+ this.resultPointCallback = resultPointCallback;\r
+ }\r
+ \r
+ /// <summary> <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since\r
+ /// it's pretty performance-critical and so is written to be fast foremost.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <returns> {@link AlignmentPattern} if found\r
+ /// </returns>\r
+ /// <throws> ReaderException if not found </throws>\r
+ internal AlignmentPattern find()\r
+ {\r
+ int startX = this.startX;\r
+ int height = this.height;\r
+ int maxJ = startX + width;\r
+ int middleI = startY + (height >> 1);\r
+ // We are looking for black/white/black modules in 1:1:1 ratio;\r
+ // this tracks the number of black/white/black modules seen so far\r
+ int[] stateCount = new int[3];\r
+ for (int iGen = 0; iGen < height; iGen++)\r
+ {\r
+ // Search from middle outwards\r
+ int i = middleI + ((iGen & 0x01) == 0?((iGen + 1) >> 1):- ((iGen + 1) >> 1));\r
+ stateCount[0] = 0;\r
+ stateCount[1] = 0;\r
+ stateCount[2] = 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 (j < maxJ && !image.get_Renamed(j, i))\r
+ {\r
+ j++;\r
+ }\r
+ int currentState = 0;\r
+ while (j < maxJ)\r
+ {\r
+ if (image.get_Renamed(j, i))\r
+ {\r
+ // Black pixel\r
+ if (currentState == 1)\r
+ {\r
+ // Counting black pixels\r
+ stateCount[currentState]++;\r
+ }\r
+ else\r
+ {\r
+ // Counting white pixels\r
+ if (currentState == 2)\r
+ {\r
+ // A winner?\r
+ if (foundPatternCross(stateCount))\r
+ {\r
+ // Yes\r
+ AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, j);\r
+ if (confirmed != null)\r
+ {\r
+ return confirmed;\r
+ }\r
+ }\r
+ stateCount[0] = stateCount[2];\r
+ stateCount[1] = 1;\r
+ stateCount[2] = 0;\r
+ currentState = 1;\r
+ }\r
+ else\r
+ {\r
+ stateCount[++currentState]++;\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // White pixel\r
+ if (currentState == 1)\r
+ {\r
+ // Counting black pixels\r
+ currentState++;\r
+ }\r
+ stateCount[currentState]++;\r
+ }\r
+ j++;\r
+ }\r
+ if (foundPatternCross(stateCount))\r
+ {\r
+ AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, maxJ);\r
+ if (confirmed != null)\r
+ {\r
+ return confirmed;\r
+ }\r
+ }\r
+ }\r
+ \r
+ // Hmm, nothing we saw was observed and confirmed twice. If we had\r
+ // any guess at all, return it.\r
+ if (!(possibleCenters.Count == 0))\r
+ {\r
+ return (AlignmentPattern) possibleCenters[0];\r
+ }\r
+ \r
+ throw ReaderException.Instance;\r
+ }\r
+ \r
+ /// <summary> Given a count of black/white/black pixels just seen and an end position,\r
+ /// figures the location of the center of this black/white/black 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[2]) - stateCount[1] / 2.0f;\r
+ }\r
+ \r
+ /// <param name="stateCount">count of black/white/black pixels just read\r
+ /// </param>\r
+ /// <returns> 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
+ /// </returns>\r
+ private bool foundPatternCross(int[] stateCount)\r
+ {\r
+ float moduleSize = this.moduleSize;\r
+ float maxVariance = moduleSize / 2.0f;\r
+ for (int i = 0; i < 3; i++)\r
+ {\r
+ if (System.Math.Abs(moduleSize - stateCount[i]) >= maxVariance)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+ \r
+ /// <summary> <p>After a horizontal scan finds a potential alignment pattern, this method\r
+ /// "cross-checks" by scanning down vertically through the center of the possible\r
+ /// alignment pattern to see if the same proportion is detected.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="startI">row where an alignment pattern was detected\r
+ /// </param>\r
+ /// <param name="centerJ">center of the section that appears to cross an alignment 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 alignment 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
+ stateCount[0] = 0;\r
+ stateCount[1] = 0;\r
+ stateCount[2] = 0;\r
+ \r
+ // Start counting up from center\r
+ int i = startI;\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) && stateCount[1] <= maxCount)\r
+ {\r
+ stateCount[1]++;\r
+ i++;\r
+ }\r
+ if (i == maxI || stateCount[1] > maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ while (i < maxI && !image.get_Renamed(centerJ, i) && stateCount[2] <= maxCount)\r
+ {\r
+ stateCount[2]++;\r
+ i++;\r
+ }\r
+ if (stateCount[2] > maxCount)\r
+ {\r
+ return System.Single.NaN;\r
+ }\r
+ \r
+ int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];\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>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 see if this pattern had been\r
+ /// found on a previous horizontal scan. If so, we consider it confirmed and conclude we have\r
+ /// found the alignment pattern.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="stateCount">reading state module counts from horizontal scan\r
+ /// </param>\r
+ /// <param name="i">row where alignment pattern may be found\r
+ /// </param>\r
+ /// <param name="j">end of possible alignment pattern in row\r
+ /// </param>\r
+ /// <returns> {@link AlignmentPattern} if we have found the same pattern twice, or null if not\r
+ /// </returns>\r
+ private AlignmentPattern handlePossibleCenter(int[] stateCount, int i, int j)\r
+ {\r
+ int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];\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, 2 * stateCount[1], stateCountTotal);\r
+ if (!System.Single.IsNaN(centerI))\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) (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f;\r
+ int max = possibleCenters.Count;\r
+ for (int index = 0; index < max; index++)\r
+ {\r
+ AlignmentPattern center = (AlignmentPattern) possibleCenters[index];\r
+ // Look for about the same center and module size:\r
+ if (center.aboutEquals(estimatedModuleSize, centerI, centerJ))\r
+ {\r
+ return new AlignmentPattern(centerJ, centerI, estimatedModuleSize);\r
+ }\r
+ }\r
+ // Hadn't found this before; save it\r
+ ResultPoint point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize);\r
+ possibleCenters.Add(point);\r
+ if (resultPointCallback != null)\r
+ {\r
+ resultPointCallback.foundPossibleResultPoint(point);\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ }\r
+}
\ No newline at end of file