Example #1
0
        private static BitMatrix sampleGrid(BitMatrix matrix, ResultPoint topLeft, ResultPoint bottomLeft, ResultPoint topRight, ResultPoint bottomRight, int dimension)
        {
            // Note that unlike the QR Code sampler, we didn't find the center of modules, but the
            // very corners. So there is no 0.5f here; 0.0f is right.
            GridSampler sampler = GridSampler.Instance;

            return(sampler.sampleGrid(matrix, dimension, 0.0f, 0.0f, dimension, 0.0f, dimension, dimension, 0.0f, dimension, topLeft.X, topLeft.Y, topRight.X, topRight.Y, bottomRight.X, bottomRight.Y, bottomLeft.X, bottomLeft.Y));            // p4FromY
        }
Example #2
0
        /// <summary> <p>Estimates module size (pixels in a module) based on the Start and End
        /// finder patterns.</p>
        ///
        /// </summary>
        /// <param name="vertices">an array of vertices:
        /// vertices[0] x, y top left barcode
        /// vertices[1] x, y bottom left barcode
        /// vertices[2] x, y top right barcode
        /// vertices[3] x, y bottom right barcode
        /// vertices[4] x, y top left codeword area
        /// vertices[5] x, y bottom left codeword area
        /// vertices[6] x, y top right codeword area
        /// vertices[7] x, y bottom right codeword area
        /// </param>
        /// <returns> the module size.
        /// </returns>
        private static float computeModuleWidth(ResultPoint[] vertices)
        {
            float pixels1      = ResultPoint.distance(vertices[0], vertices[4]);
            float pixels2      = ResultPoint.distance(vertices[1], vertices[5]);
            float moduleWidth1 = (pixels1 + pixels2) / (17 * 2.0f);
            float pixels3      = ResultPoint.distance(vertices[6], vertices[2]);
            float pixels4      = ResultPoint.distance(vertices[7], vertices[3]);
            float moduleWidth2 = (pixels3 + pixels4) / (18 * 2.0f);

            return((moduleWidth1 + moduleWidth2) / 2.0f);
        }
Example #3
0
        /// <summary> Because we scan horizontally to detect the start and stop patterns, the vertical component of
        /// the codeword coordinates will be slightly wrong if there is any skew or rotation in the image.
        /// This method moves those points back onto the edges of the theoretically perfect bounding
        /// quadrilateral if needed.
        ///
        /// </summary>
        /// <param name="vertices">The eight vertices located by findVertices().
        /// </param>
        private static void  correctCodeWordVertices(ResultPoint[] vertices, bool upsideDown)
        {
            float skew = vertices[4].Y - vertices[6].Y;

            if (upsideDown)
            {
                skew = -skew;
            }
            if (skew > SKEW_THRESHOLD)
            {
                // Fix v4
                float length     = vertices[4].X - vertices[0].X;
                float deltax     = vertices[6].X - vertices[0].X;
                float deltay     = vertices[6].Y - vertices[0].Y;
                float correction = length * deltay / deltax;
                vertices[4] = new ResultPoint(vertices[4].X, vertices[4].Y + correction);
            }
            else if (-skew > SKEW_THRESHOLD)
            {
                // Fix v6
                float length     = vertices[2].X - vertices[6].X;
                float deltax     = vertices[2].X - vertices[4].X;
                float deltay     = vertices[2].Y - vertices[4].Y;
                float correction = length * deltay / deltax;
                vertices[6] = new ResultPoint(vertices[6].X, vertices[6].Y - correction);
            }

            skew = vertices[7].Y - vertices[5].Y;
            if (upsideDown)
            {
                skew = -skew;
            }
            if (skew > SKEW_THRESHOLD)
            {
                // Fix v5
                float length     = vertices[5].X - vertices[1].X;
                float deltax     = vertices[7].X - vertices[1].X;
                float deltay     = vertices[7].Y - vertices[1].Y;
                float correction = length * deltay / deltax;
                vertices[5] = new ResultPoint(vertices[5].X, vertices[5].Y + correction);
            }
            else if (-skew > SKEW_THRESHOLD)
            {
                // Fix v7
                float length     = vertices[3].X - vertices[7].X;
                float deltax     = vertices[3].X - vertices[5].X;
                float deltay     = vertices[3].Y - vertices[5].Y;
                float correction = length * deltay / deltax;
                vertices[7] = new ResultPoint(vertices[7].X, vertices[7].Y - correction);
            }
        }
Example #4
0
        /// <summary> Computes the dimension (number of modules in a row) of the PDF417 Code
        /// based on vertices of the codeword area and estimated module size.
        ///
        /// </summary>
        /// <param name="topLeft">    of codeword area
        /// </param>
        /// <param name="topRight">   of codeword area
        /// </param>
        /// <param name="bottomLeft"> of codeword area
        /// </param>
        /// <param name="bottomRight">of codeword are
        /// </param>
        /// <param name="moduleWidth">estimated module size
        /// </param>
        /// <returns> the number of modules in a row.
        /// </returns>
        private static int computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, ResultPoint bottomRight, float moduleWidth)
        {
            int topRowDimension    = round(ResultPoint.distance(topLeft, topRight) / moduleWidth);
            int bottomRowDimension = round(ResultPoint.distance(bottomLeft, bottomRight) / moduleWidth);

            return(((((topRowDimension + bottomRowDimension) >> 1) + 8) / 17) * 17);

            /*
             * int topRowDimension = round(ResultPoint.distance(topLeft,
             * topRight)); //moduleWidth); int bottomRowDimension =
             * round(ResultPoint.distance(bottomLeft, bottomRight)); //
             * moduleWidth); int dimension = ((topRowDimension + bottomRowDimension)
             * >> 1); // Round up to nearest 17 modules i.e. there are 17 modules per
             * codeword //int dimension = ((((topRowDimension + bottomRowDimension) >>
             * 1) + 8) / 17) * 17; return dimension;
             */
        }
Example #5
0
		private static BitMatrix sampleGrid(BitMatrix matrix, ResultPoint topLeft, ResultPoint bottomLeft, ResultPoint topRight, ResultPoint bottomRight, int dimension)
		{
			
			// Note that unlike the QR Code sampler, we didn't find the center of modules, but the
			// very corners. So there is no 0.5f here; 0.0f is right.
			GridSampler sampler = GridSampler.Instance;
			
			return sampler.sampleGrid(matrix, dimension, 0.0f, 0.0f, dimension, 0.0f, dimension, dimension, 0.0f, dimension, topLeft.X, topLeft.Y, topRight.X, topRight.Y, bottomRight.X, bottomRight.Y, bottomLeft.X, bottomLeft.Y); // p4FromY
		}
Example #6
0
		/// <summary> Computes the dimension (number of modules in a row) of the PDF417 Code
		/// based on vertices of the codeword area and estimated module size.
		/// 
		/// </summary>
		/// <param name="topLeft">    of codeword area
		/// </param>
		/// <param name="topRight">   of codeword area
		/// </param>
		/// <param name="bottomLeft"> of codeword area
		/// </param>
		/// <param name="bottomRight">of codeword are
		/// </param>
		/// <param name="moduleWidth">estimated module size
		/// </param>
		/// <returns> the number of modules in a row.
		/// </returns>
		private static int computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, ResultPoint bottomRight, float moduleWidth)
		{
			int topRowDimension = round(ResultPoint.distance(topLeft, topRight) / moduleWidth);
			int bottomRowDimension = round(ResultPoint.distance(bottomLeft, bottomRight) / moduleWidth);
			return ((((topRowDimension + bottomRowDimension) >> 1) + 8) / 17) * 17;
			/*
			* int topRowDimension = round(ResultPoint.distance(topLeft,
			* topRight)); //moduleWidth); int bottomRowDimension =
			* round(ResultPoint.distance(bottomLeft, bottomRight)); //
			* moduleWidth); int dimension = ((topRowDimension + bottomRowDimension)
			* >> 1); // Round up to nearest 17 modules i.e. there are 17 modules per
			* codeword //int dimension = ((((topRowDimension + bottomRowDimension) >>
			* 1) + 8) / 17) * 17; return dimension;
			*/
		}
Example #7
0
		/// <summary> <p>Estimates module size (pixels in a module) based on the Start and End
		/// finder patterns.</p>
		/// 
		/// </summary>
		/// <param name="vertices">an array of vertices:
		/// vertices[0] x, y top left barcode
		/// vertices[1] x, y bottom left barcode
		/// vertices[2] x, y top right barcode
		/// vertices[3] x, y bottom right barcode
		/// vertices[4] x, y top left codeword area
		/// vertices[5] x, y bottom left codeword area
		/// vertices[6] x, y top right codeword area
		/// vertices[7] x, y bottom right codeword area
		/// </param>
		/// <returns> the module size.
		/// </returns>
		private static float computeModuleWidth(ResultPoint[] vertices)
		{
			float pixels1 = ResultPoint.distance(vertices[0], vertices[4]);
			float pixels2 = ResultPoint.distance(vertices[1], vertices[5]);
			float moduleWidth1 = (pixels1 + pixels2) / (17 * 2.0f);
			float pixels3 = ResultPoint.distance(vertices[6], vertices[2]);
			float pixels4 = ResultPoint.distance(vertices[7], vertices[3]);
			float moduleWidth2 = (pixels3 + pixels4) / (18 * 2.0f);
			return (moduleWidth1 + moduleWidth2) / 2.0f;
		}
Example #8
0
		/// <summary> Because we scan horizontally to detect the start and stop patterns, the vertical component of
		/// the codeword coordinates will be slightly wrong if there is any skew or rotation in the image.
		/// This method moves those points back onto the edges of the theoretically perfect bounding
		/// quadrilateral if needed.
		/// 
		/// </summary>
		/// <param name="vertices">The eight vertices located by findVertices().
		/// </param>
		private static void  correctCodeWordVertices(ResultPoint[] vertices, bool upsideDown)
		{
			float skew = vertices[4].Y - vertices[6].Y;
			if (upsideDown)
			{
				skew = - skew;
			}
			if (skew > SKEW_THRESHOLD)
			{
				// Fix v4
				float length = vertices[4].X - vertices[0].X;
				float deltax = vertices[6].X - vertices[0].X;
				float deltay = vertices[6].Y - vertices[0].Y;
				float correction = length * deltay / deltax;
				vertices[4] = new ResultPoint(vertices[4].X, vertices[4].Y + correction);
			}
			else if (- skew > SKEW_THRESHOLD)
			{
				// Fix v6
				float length = vertices[2].X - vertices[6].X;
				float deltax = vertices[2].X - vertices[4].X;
				float deltay = vertices[2].Y - vertices[4].Y;
				float correction = length * deltay / deltax;
				vertices[6] = new ResultPoint(vertices[6].X, vertices[6].Y - correction);
			}
			
			skew = vertices[7].Y - vertices[5].Y;
			if (upsideDown)
			{
				skew = - skew;
			}
			if (skew > SKEW_THRESHOLD)
			{
				// Fix v5
				float length = vertices[5].X - vertices[1].X;
				float deltax = vertices[7].X - vertices[1].X;
				float deltay = vertices[7].Y - vertices[1].Y;
				float correction = length * deltay / deltax;
				vertices[5] = new ResultPoint(vertices[5].X, vertices[5].Y + correction);
			}
			else if (- skew > SKEW_THRESHOLD)
			{
				// Fix v7
				float length = vertices[3].X - vertices[7].X;
				float deltax = vertices[3].X - vertices[5].X;
				float deltay = vertices[3].Y - vertices[5].Y;
				float correction = length * deltay / deltax;
				vertices[7] = new ResultPoint(vertices[7].X, vertices[7].Y - correction);
			}
		}
Example #9
0
		/// <summary> Locate the vertices and the codewords area of a black blob using the Start
		/// and Stop patterns as locators. This assumes that the image is rotated 180
		/// degrees and if it locates the start and stop patterns at it will re-map
		/// the vertices for a 0 degree rotation.
		/// TODO: Change assumption about barcode location.
		/// TODO: Scanning every row is very expensive. We should only do this for TRY_HARDER.
		/// 
		/// </summary>
		/// <param name="matrix">the scanned barcode image.
		/// </param>
		/// <returns> an array containing the vertices:
		/// vertices[0] x, y top left barcode
		/// vertices[1] x, y bottom left barcode
		/// vertices[2] x, y top right barcode
		/// vertices[3] x, y bottom right barcode
		/// vertices[4] x, y top left codeword area
		/// vertices[5] x, y bottom left codeword area
		/// vertices[6] x, y top right codeword area
		/// vertices[7] x, y bottom right codeword area
		/// </returns>
		private static ResultPoint[] findVertices180(BitMatrix matrix)
		{
			int height = matrix.Height;
			int width = matrix.Width;
			int halfWidth = width >> 1;
			
			ResultPoint[] result = new ResultPoint[8];
			bool found = false;
			
			// Top Left
			for (int i = height - 1; i > 0; i--)
			{
				int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
				if (loc != null)
				{
					result[0] = new ResultPoint(loc[1], i);
					result[4] = new ResultPoint(loc[0], i);
					found = true;
					break;
				}
			}
			// Bottom Left
			if (found)
			{
				// Found the Top Left vertex
				found = false;
				for (int i = 0; i < height; i++)
				{
					int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
					if (loc != null)
					{
						result[1] = new ResultPoint(loc[1], i);
						result[5] = new ResultPoint(loc[0], i);
						found = true;
						break;
					}
				}
			}
			// Top Right
			if (found)
			{
				// Found the Bottom Left vertex
				found = false;
				for (int i = height - 1; i > 0; i--)
				{
					int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
					if (loc != null)
					{
						result[2] = new ResultPoint(loc[0], i);
						result[6] = new ResultPoint(loc[1], i);
						found = true;
						break;
					}
				}
			}
			// Bottom Right
			if (found)
			{
				// Found the Top Right vertex
				found = false;
				for (int i = 0; i < height; i++)
				{
					int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
					if (loc != null)
					{
						result[3] = new ResultPoint(loc[0], i);
						result[7] = new ResultPoint(loc[1], i);
						found = true;
						break;
					}
				}
			}
			return found?result:null;
		}
Example #10
0
        /// <summary> Locate the vertices and the codewords area of a black blob using the Start
        /// and Stop patterns as locators. This assumes that the image is rotated 180
        /// degrees and if it locates the start and stop patterns at it will re-map
        /// the vertices for a 0 degree rotation.
        /// TODO: Change assumption about barcode location.
        /// TODO: Scanning every row is very expensive. We should only do this for TRY_HARDER.
        ///
        /// </summary>
        /// <param name="matrix">the scanned barcode image.
        /// </param>
        /// <returns> an array containing the vertices:
        /// vertices[0] x, y top left barcode
        /// vertices[1] x, y bottom left barcode
        /// vertices[2] x, y top right barcode
        /// vertices[3] x, y bottom right barcode
        /// vertices[4] x, y top left codeword area
        /// vertices[5] x, y bottom left codeword area
        /// vertices[6] x, y top right codeword area
        /// vertices[7] x, y bottom right codeword area
        /// </returns>
        private static ResultPoint[] findVertices180(BitMatrix matrix)
        {
            int height    = matrix.Height;
            int width     = matrix.Width;
            int halfWidth = width >> 1;

            ResultPoint[] result = new ResultPoint[8];
            bool          found  = false;

            // Top Left
            for (int i = height - 1; i > 0; i--)
            {
                int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
                if (loc != null)
                {
                    result[0] = new ResultPoint(loc[1], i);
                    result[4] = new ResultPoint(loc[0], i);
                    found     = true;
                    break;
                }
            }
            // Bottom Left
            if (found)
            {
                // Found the Top Left vertex
                found = false;
                for (int i = 0; i < height; i++)
                {
                    int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
                    if (loc != null)
                    {
                        result[1] = new ResultPoint(loc[1], i);
                        result[5] = new ResultPoint(loc[0], i);
                        found     = true;
                        break;
                    }
                }
            }
            // Top Right
            if (found)
            {
                // Found the Bottom Left vertex
                found = false;
                for (int i = height - 1; i > 0; i--)
                {
                    int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
                    if (loc != null)
                    {
                        result[2] = new ResultPoint(loc[0], i);
                        result[6] = new ResultPoint(loc[1], i);
                        found     = true;
                        break;
                    }
                }
            }
            // Bottom Right
            if (found)
            {
                // Found the Top Right vertex
                found = false;
                for (int i = 0; i < height; i++)
                {
                    int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
                    if (loc != null)
                    {
                        result[3] = new ResultPoint(loc[0], i);
                        result[7] = new ResultPoint(loc[1], i);
                        found     = true;
                        break;
                    }
                }
            }
            return(found?result:null);
        }
Example #11
0
        internal virtual FinderPatternInfo find(System.Collections.Hashtable hints)
        {
            bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER);
            int  maxI      = image.Height;
            int  maxJ      = image.Width;
            // We are looking for black/white/black/white/black modules in
            // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far

            // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
            // image, and then account for the center being 3 modules in size. This gives the smallest
            // number of pixels the center could be, so skip this often. When trying harder, look for all
            // QR versions regardless of how dense they are.
            int iSkip = (3 * maxI) / (4 * MAX_MODULES);

            if (iSkip < MIN_SKIP || tryHarder)
            {
                iSkip = MIN_SKIP;
            }

            bool done = false;

            int[] stateCount = new int[5];
            for (int i = iSkip - 1; i < maxI && !done; i += iSkip)
            {
                // Get a row of black/white values
                stateCount[0] = 0;
                stateCount[1] = 0;
                stateCount[2] = 0;
                stateCount[3] = 0;
                stateCount[4] = 0;
                int currentState = 0;
                for (int j = 0; j < maxJ; j++)
                {
                    if (image.get_Renamed(j, i))
                    {
                        // Black pixel
                        if ((currentState & 1) == 1)
                        {
                            // Counting white pixels
                            currentState++;
                        }
                        stateCount[currentState]++;
                    }
                    else
                    {
                        // White pixel
                        if ((currentState & 1) == 0)
                        {
                            // Counting black pixels
                            if (currentState == 4)
                            {
                                // A winner?
                                if (foundPatternCross(stateCount))
                                {
                                    // Yes
                                    bool confirmed = handlePossibleCenter(stateCount, i, j);
                                    if (confirmed)
                                    {
                                        // Start examining every other line. Checking each line turned out to be too
                                        // expensive and didn't improve performance.
                                        iSkip = 2;
                                        if (hasSkipped)
                                        {
                                            done = haveMultiplyConfirmedCenters();
                                        }
                                        else
                                        {
                                            int rowSkip = findRowSkip();
                                            if (rowSkip > stateCount[2])
                                            {
                                                // Skip rows between row of lower confirmed center
                                                // and top of presumed third confirmed center
                                                // but back up a bit to get a full chance of detecting
                                                // it, entire width of center of finder pattern

                                                // Skip by rowSkip, but back off by stateCount[2] (size of last center
                                                // of pattern we saw) to be conservative, and also back off by iSkip which
                                                // is about to be re-added
                                                i += rowSkip - stateCount[2] - iSkip;
                                                j  = maxJ - 1;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // Advance to next black pixel
                                        do
                                        {
                                            j++;
                                        }while (j < maxJ && !image.get_Renamed(j, i));
                                        j--;                                         // back up to that last white pixel
                                    }
                                    // Clear state to start looking again
                                    currentState  = 0;
                                    stateCount[0] = 0;
                                    stateCount[1] = 0;
                                    stateCount[2] = 0;
                                    stateCount[3] = 0;
                                    stateCount[4] = 0;
                                }
                                else
                                {
                                    // No, shift counts back by two
                                    stateCount[0] = stateCount[2];
                                    stateCount[1] = stateCount[3];
                                    stateCount[2] = stateCount[4];
                                    stateCount[3] = 1;
                                    stateCount[4] = 0;
                                    currentState  = 3;
                                }
                            }
                            else
                            {
                                stateCount[++currentState]++;
                            }
                        }
                        else
                        {
                            // Counting white pixels
                            stateCount[currentState]++;
                        }
                    }
                }
                if (foundPatternCross(stateCount))
                {
                    bool confirmed = handlePossibleCenter(stateCount, i, maxJ);
                    if (confirmed)
                    {
                        iSkip = stateCount[0];
                        if (hasSkipped)
                        {
                            // Found a third one
                            done = haveMultiplyConfirmedCenters();
                        }
                    }
                }
            }

            FinderPattern[] patternInfo = selectBestPatterns();
            ResultPoint.orderBestPatterns(patternInfo);

            return(new FinderPatternInfo(patternInfo));
        }
        public FinderPatternInfo[] findMulti(System.Collections.Hashtable hints)
        {
            bool      tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER);
            BitMatrix image     = Image;
            int       maxI      = image.Height;
            int       maxJ      = image.Width;
            // We are looking for black/white/black/white/black modules in
            // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far

            // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
            // image, and then account for the center being 3 modules in size. This gives the smallest
            // number of pixels the center could be, so skip this often. When trying harder, look for all
            // QR versions regardless of how dense they are.
            //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'"
            int iSkip = (int)(maxI / (MAX_MODULES * 4.0f) * 3);

            if (iSkip < MIN_SKIP || tryHarder)
            {
                iSkip = MIN_SKIP;
            }

            int[] stateCount = new int[5];
            for (int i = iSkip - 1; i < maxI; i += iSkip)
            {
                // Get a row of black/white values
                stateCount[0] = 0;
                stateCount[1] = 0;
                stateCount[2] = 0;
                stateCount[3] = 0;
                stateCount[4] = 0;
                int currentState = 0;
                for (int j = 0; j < maxJ; j++)
                {
                    if (image.get_Renamed(j, i))
                    {
                        // Black pixel
                        if ((currentState & 1) == 1)
                        {
                            // Counting white pixels
                            currentState++;
                        }
                        stateCount[currentState]++;
                    }
                    else
                    {
                        // White pixel
                        if ((currentState & 1) == 0)
                        {
                            // Counting black pixels
                            if (currentState == 4)
                            {
                                // A winner?
                                if (foundPatternCross(stateCount))
                                {
                                    // Yes
                                    bool confirmed = handlePossibleCenter(stateCount, i, j);
                                    if (!confirmed)
                                    {
                                        do
                                        {
                                            // Advance to next black pixel
                                            j++;
                                        }while (j < maxJ && !image.get_Renamed(j, i));
                                        j--;                                         // back up to that last white pixel
                                    }
                                    // Clear state to start looking again
                                    currentState  = 0;
                                    stateCount[0] = 0;
                                    stateCount[1] = 0;
                                    stateCount[2] = 0;
                                    stateCount[3] = 0;
                                    stateCount[4] = 0;
                                }
                                else
                                {
                                    // No, shift counts back by two
                                    stateCount[0] = stateCount[2];
                                    stateCount[1] = stateCount[3];
                                    stateCount[2] = stateCount[4];
                                    stateCount[3] = 1;
                                    stateCount[4] = 0;
                                    currentState  = 3;
                                }
                            }
                            else
                            {
                                stateCount[++currentState]++;
                            }
                        }
                        else
                        {
                            // Counting white pixels
                            stateCount[currentState]++;
                        }
                    }
                }                 // for j=...

                if (foundPatternCross(stateCount))
                {
                    handlePossibleCenter(stateCount, i, maxJ);
                }         // end if foundPatternCross
            }             // for i=iSkip-1 ...
            FinderPattern[][]            patternInfo = selectBestPatterns();
            System.Collections.ArrayList result      = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
            for (int i = 0; i < patternInfo.Length; i++)
            {
                FinderPattern[] pattern = patternInfo[i];
                ResultPoint.orderBestPatterns(pattern);
                result.Add(new FinderPatternInfo(pattern));
            }

            if ((result.Count == 0))
            {
                return(EMPTY_RESULT_ARRAY);
            }
            else
            {
                FinderPatternInfo[] resultArray = new FinderPatternInfo[result.Count];
                for (int i = 0; i < result.Count; i++)
                {
                    resultArray[i] = (FinderPatternInfo)result[i];
                }
                return(resultArray);
            }
        }
        /// <returns> the 3 best {@link FinderPattern}s from our list of candidates. The "best" are
        /// those that have been detected at least {@link #CENTER_QUORUM} times, and whose module
        /// size differs from the average among those patterns the least
        /// </returns>
        /// <throws>  ReaderException if 3 such finder patterns do not exist </throws>
        private FinderPattern[][] selectBestPatterns()
        {
            System.Collections.ArrayList possibleCenters = PossibleCenters;
            int size = possibleCenters.Count;

            if (size < 3)
            {
                // Couldn't find enough finder patterns
                throw ReaderException.Instance;
            }

            /*
             * Begin HE modifications to safely detect multiple codes of equal size
             */
            if (size == 3)
            {
                return(new FinderPattern[][] { new FinderPattern[] { (FinderPattern)possibleCenters[0], (FinderPattern)possibleCenters[1], (FinderPattern)possibleCenters[2] } });
            }

            // Sort by estimated module size to speed up the upcoming checks
            Collections.insertionSort(possibleCenters, new ModuleSizeComparator());

            /*
             * Now lets start: build a list of tuples of three finder locations that
             *  - feature similar module sizes
             *  - are placed in a distance so the estimated module count is within the QR specification
             *  - have similar distance between upper left/right and left top/bottom finder patterns
             *  - form a triangle with 90° angle (checked by comparing top right/bottom left distance
             *    with pythagoras)
             *
             * Note: we allow each point to be used for more than one code region: this might seem
             * counterintuitive at first, but the performance penalty is not that big. At this point,
             * we cannot make a good quality decision whether the three finders actually represent
             * a QR code, or are just by chance layouted so it looks like there might be a QR code there.
             * So, if the layout seems right, lets have the decoder try to decode.
             */

            System.Collections.ArrayList results = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));             // holder for the results

            for (int i1 = 0; i1 < (size - 2); i1++)
            {
                FinderPattern p1 = (FinderPattern)possibleCenters[i1];
                if (p1 == null)
                {
                    continue;
                }

                for (int i2 = i1 + 1; i2 < (size - 1); i2++)
                {
                    FinderPattern p2 = (FinderPattern)possibleCenters[i2];
                    if (p2 == null)
                    {
                        continue;
                    }

                    // Compare the expected module sizes; if they are really off, skip
                    float vModSize12  = (p1.EstimatedModuleSize - p2.EstimatedModuleSize) / (System.Math.Min(p1.EstimatedModuleSize, p2.EstimatedModuleSize));
                    float vModSize12A = System.Math.Abs(p1.EstimatedModuleSize - p2.EstimatedModuleSize);
                    if (vModSize12A > DIFF_MODSIZE_CUTOFF && vModSize12 >= DIFF_MODSIZE_CUTOFF_PERCENT)
                    {
                        // break, since elements are ordered by the module size deviation there cannot be
                        // any more interesting elements for the given p1.
                        break;
                    }

                    for (int i3 = i2 + 1; i3 < size; i3++)
                    {
                        FinderPattern p3 = (FinderPattern)possibleCenters[i3];
                        if (p3 == null)
                        {
                            continue;
                        }

                        // Compare the expected module sizes; if they are really off, skip
                        float vModSize23  = (p2.EstimatedModuleSize - p3.EstimatedModuleSize) / (System.Math.Min(p2.EstimatedModuleSize, p3.EstimatedModuleSize));
                        float vModSize23A = System.Math.Abs(p2.EstimatedModuleSize - p3.EstimatedModuleSize);
                        if (vModSize23A > DIFF_MODSIZE_CUTOFF && vModSize23 >= DIFF_MODSIZE_CUTOFF_PERCENT)
                        {
                            // break, since elements are ordered by the module size deviation there cannot be
                            // any more interesting elements for the given p1.
                            break;
                        }

                        FinderPattern[] test = new FinderPattern[] { p1, p2, p3 };
                        ResultPoint.orderBestPatterns(test);

                        // Calculate the distances: a = topleft-bottomleft, b=topleft-topright, c = diagonal
                        FinderPatternInfo info = new FinderPatternInfo(test);
                        float             dA   = ResultPoint.distance(info.TopLeft, info.BottomLeft);
                        float             dC   = ResultPoint.distance(info.TopRight, info.BottomLeft);
                        float             dB   = ResultPoint.distance(info.TopLeft, info.TopRight);

                        // Check the sizes
                        float estimatedModuleCount = ((dA + dB) / p1.EstimatedModuleSize) / 2;
                        if (estimatedModuleCount > MAX_MODULE_COUNT_PER_EDGE || estimatedModuleCount < MIN_MODULE_COUNT_PER_EDGE)
                        {
                            continue;
                        }

                        // Calculate the difference of the edge lengths in percent
                        float vABBC = System.Math.Abs(((dA - dB) / System.Math.Min(dA, dB)));
                        if (vABBC >= 0.1f)
                        {
                            continue;
                        }

                        // Calculate the diagonal length by assuming a 90° angle at topleft
                        //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'"
                        float dCpy = (float)System.Math.Sqrt(dA * dA + dB * dB);
                        // Compare to the real distance in %
                        float vPyC = System.Math.Abs(((dC - dCpy) / System.Math.Min(dC, dCpy)));

                        if (vPyC >= 0.1f)
                        {
                            continue;
                        }

                        // All tests passed!
                        results.Add(test);
                    }     // end iterate p3
                }         // end iterate p2
            }             // end iterate p1

            if (!(results.Count == 0))
            {
                FinderPattern[][] resultArray = new FinderPattern[results.Count][];
                for (int i = 0; i < results.Count; i++)
                {
                    resultArray[i] = (FinderPattern[])results[i];
                }
                return(resultArray);
            }

            // Nothing found!
            throw ReaderException.Instance;
        }