コード例 #1
0
        /// <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>
        private FinderPattern[] SelectBestPatterns()
        {
            int startSize = PossibleCenters.Count;

            if (startSize < 3)
            {
                // Couldn't find enough finder patterns
                return(null);
            }

            // Filter outlier possibilities whose module size is too different
            if (startSize > 3)
            {
                // But we can only afford to do so if we have at least 4 possibilities to choose from
                float totalModuleSize = 0.0f;
                float square          = 0.0f;
                foreach (var center in PossibleCenters)
                {
                    float size = center.EstimatedModuleSize;
                    totalModuleSize += size;
                    square          += size * size;
                }
                float average = totalModuleSize / startSize;
                float stdDev  = (float)Math.Sqrt(square / startSize - average * average);

                PossibleCenters.Sort(new FurthestFromAverageComparator(average));

                float limit = Math.Max(0.2f * average, stdDev);

                for (int i = 0; i < PossibleCenters.Count && PossibleCenters.Count > 3; i++)
                {
                    FinderPattern pattern = PossibleCenters[i];
                    if (Math.Abs(pattern.EstimatedModuleSize - average) > limit)
                    {
                        PossibleCenters.RemoveAt(i);
                        i--;
                    }
                }
            }

            if (PossibleCenters.Count > 3)
            {
                // Throw away all but those first size candidate points we found.

                float totalModuleSize = 0.0f;
                foreach (var possibleCenter in PossibleCenters)
                {
                    totalModuleSize += possibleCenter.EstimatedModuleSize;
                }

                float average = totalModuleSize / PossibleCenters.Count;

                PossibleCenters.Sort(new CenterComparator(average));

                //possibleCenters.subList(3, possibleCenters.Count).clear();
                PossibleCenters = PossibleCenters.GetRange(0, 3);
            }

            return(new[] { PossibleCenters[0], PossibleCenters[1], PossibleCenters[2] });
        }
コード例 #2
0
        /// <summary>
        ///   <p>This is called when a horizontal scan finds a possible alignment pattern. It will
        /// cross check with a vertical scan, and if successful, will, ah, cross-cross-check
        /// with another horizontal scan. This is needed primarily to locate the real horizontal
        /// center of the pattern in cases of extreme skew.
        /// And then we cross-cross-cross check with another diagonal scan.</p>
        /// If that succeeds the finder pattern location is added to a list that tracks
        /// the number of times each location has been nearly-matched as a finder pattern.
        /// Each additional find is more evidence that the location is in fact a finder
        /// pattern center
        /// </summary>
        /// <param name="stateCount">reading state module counts from horizontal scan</param>
        /// <param name="i">row where finder pattern may be found</param>
        /// <param name="j">end of possible finder pattern in row</param>
        /// <param name="pureBarcode">true if in "pure barcode" mode</param>
        /// <returns>
        /// true if a finder pattern candidate was found this time
        /// </returns>
        bool HandlePossibleCenter(int[] stateCount, int i, int j)
        {
            int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
                                  stateCount[4];
            float?centerJ = CenterFromEnd(stateCount, j);

            if (centerJ == null)
            {
                return(false);
            }
            float?centerI = CrossCheckVertical(i, (int)centerJ.Value, stateCount[2], stateCountTotal);

            if (centerI != null)
            {
                // Re-cross check
                centerJ = CrossCheckHorizontal((int)centerJ.Value, (int)centerI.Value, stateCount[2], stateCountTotal);
                if (centerJ != null)
                {
                    float estimatedModuleSize = stateCountTotal / 7.0f;
                    bool  found = false;
                    for (int index = 0; index < PossibleCenters.Count; index++)
                    {
                        var center = PossibleCenters[index];
                        // Look for about the same center and module size:
                        if (center.AboutEquals(estimatedModuleSize, centerI.Value, centerJ.Value))
                        {
                            PossibleCenters.RemoveAt(index);
                            PossibleCenters.Insert(index, center.CombineEstimate(centerI.Value, centerJ.Value, estimatedModuleSize));

                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        var point = new FinderPattern(centerJ.Value, centerI.Value, estimatedModuleSize);

                        PossibleCenters.Add(point);
                    }
                    return(true);
                }
            }
            return(false);
        }
コード例 #3
0
        /// <summary> 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
        /// </summary>
        /// <throws>  ReaderException if 3 such finder patterns do not exist </throws>
        private FinderPattern[] SelectBestPatterns()
        {
            int startSize = PossibleCenters.Count;

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

            // Filter outlier possibilities whose module size is too different
            if (startSize > 3)
            {
                // But we can only afford to do so if we have at least 4 possibilities to choose from
                float totalModuleSize = 0.0f;
                for (int i = 0; i < startSize; i++)
                {
                    totalModuleSize += ((FinderPattern)PossibleCenters[i]).EstimatedModuleSize;
                }
                //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 average = totalModuleSize / startSize;
                for (int i = 0; i < PossibleCenters.Count && PossibleCenters.Count > 3; i++)
                {
                    FinderPattern pattern = (FinderPattern)PossibleCenters[i];
                    if (Math.Abs(pattern.EstimatedModuleSize - average) > 0.2f * average)
                    {
                        PossibleCenters.RemoveAt(i);
                        i--;
                    }
                }
            }

            if (PossibleCenters.Count > 3)
            {
                // Throw away all but those first size candidate points we found.
                Collections.InsertionSort(PossibleCenters, new CenterComparator());
                SupportClass.SetCapacity(PossibleCenters, 3);
            }

            return(new[] { (FinderPattern)PossibleCenters[0], (FinderPattern)PossibleCenters[1], (FinderPattern)PossibleCenters[2] });
        }
コード例 #4
0
        /// <summary> <para>
        /// <p>This is called when a horizontal scan finds a possible alignment pattern. It will
        /// cross check with a vertical scan, and if successful, will, ah, cross-cross-check
        /// with another horizontal scan. This is needed primarily to locate the real horizontal
        /// center of the pattern in cases of extreme skew.</p>
        /// </para>
        /// <para>
        /// <p>If that succeeds the finder pattern location is added to a list that tracks
        /// the number of times each location has been nearly-matched as a finder pattern.
        /// Each additional find is more evidence that the location is in fact a finder
        /// pattern center</p>
        /// </para>
        ///
        /// </summary>
        /// <param name="stateCount">reading state module counts from horizontal scan
        /// </param>
        /// <param name="i">row where finder pattern may be found
        /// </param>
        /// <param name="j">end of possible finder pattern in row
        /// </param>
        /// <returns> true if a finder pattern candidate was found this time
        /// </returns>
        private bool HandlePossibleCenter(int[] stateCount, int i, int j)
        {
            int   stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];
            float centerJ         = CenterFromEnd(stateCount, j);
            //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 centerI = CrossCheckVertical(i, (int)centerJ, stateCount[2], stateCountTotal);

            if (!float.IsNaN(centerI))
            {
                // Re-cross check
                //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'"
                centerJ = CrossCheckHorizontal((int)centerJ, (int)centerI, stateCount[2], stateCountTotal);
                if (!float.IsNaN(centerJ))
                {
                    //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 estimatedModuleSize = stateCountTotal / 7.0f;
                    bool  found = false;
                    int   max   = PossibleCenters.Count;
                    for (int index = 0; index < max; index++)
                    {
                        FinderPattern center = (FinderPattern)PossibleCenters[index];
                        // Look for about the same center and module size:
                        if (center.AboutEquals(estimatedModuleSize, centerI, centerJ))
                        {
                            center.IncrementCount();
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        ResultPoint point = new FinderPattern(centerJ, centerI, estimatedModuleSize);
                        PossibleCenters.Add(point);
                        resultPointCallback?.FoundPossibleResultPoint(point);
                    }
                    return(true);
                }
            }
            return(false);
        }