예제 #1
0
        /// <summary> <p>Detects a Data Matrix Code in an image.</p>
        ///
        /// </summary>
        /// <returns> {@link DetectorResult} encapsulating results of detecting a QR Code
        /// </returns>
        /// <throws>  ReaderException if no Data Matrix Code can be found </throws>
        public DetectorResult detect()
        {
            ResultPoint[] cornerPoints = rectangleDetector.detect();
            ResultPoint   pointA       = cornerPoints[0];
            ResultPoint   pointB       = cornerPoints[1];
            ResultPoint   pointC       = cornerPoints[2];
            ResultPoint   pointD       = cornerPoints[3];

            // Point A and D are across the diagonal from one another,
            // as are B and C. Figure out which are the solid black lines
            // by counting transitions
            System.Collections.ArrayList transitions = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(4));
            transitions.Add(transitionsBetween(pointA, pointB));
            transitions.Add(transitionsBetween(pointA, pointC));
            transitions.Add(transitionsBetween(pointB, pointD));
            transitions.Add(transitionsBetween(pointC, pointD));
            Collections.insertionSort(transitions, new ResultPointsAndTransitionsComparator());

            // Sort by number of transitions. First two will be the two solid sides; last two
            // will be the two alternating black/white sides
            ResultPointsAndTransitions lSideOne = (ResultPointsAndTransitions)transitions[0];
            ResultPointsAndTransitions lSideTwo = (ResultPointsAndTransitions)transitions[1];

            // Figure out which point is their intersection by tallying up the number of times we see the
            // endpoints in the four endpoints. One will show up twice.
            System.Collections.Hashtable pointCount = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
            increment(pointCount, lSideOne.From);
            increment(pointCount, lSideOne.To);
            increment(pointCount, lSideTwo.From);
            increment(pointCount, lSideTwo.To);

            ResultPoint maybeTopLeft     = null;
            ResultPoint bottomLeft       = null;
            ResultPoint maybeBottomRight = null;

            System.Collections.IEnumerator points = pointCount.Keys.GetEnumerator();
            //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
            while (points.MoveNext())
            {
                //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
                ResultPoint  point         = (ResultPoint)points.Current;
                System.Int32 value_Renamed = (System.Int32)pointCount[point];
                if (value_Renamed == 2)
                {
                    bottomLeft = point;                     // this is definitely the bottom left, then -- end of two L sides
                }
                else
                {
                    // Otherwise it's either top left or bottom right -- just assign the two arbitrarily now
                    if (maybeTopLeft == null)
                    {
                        maybeTopLeft = point;
                    }
                    else
                    {
                        maybeBottomRight = point;
                    }
                }
            }

            if (maybeTopLeft == null || bottomLeft == null || maybeBottomRight == null)
            {
                throw ReaderException.Instance;
            }

            // Bottom left is correct but top left and bottom right might be switched
            ResultPoint[] corners = new ResultPoint[] { maybeTopLeft, bottomLeft, maybeBottomRight };
            // Use the dot product trick to sort them out
            ResultPoint.orderBestPatterns(corners);

            // Now we know which is which:
            ResultPoint bottomRight = corners[0];

            bottomLeft = corners[1];
            ResultPoint topLeft = corners[2];

            // Which point didn't we find in relation to the "L" sides? that's the top right corner
            ResultPoint topRight;

            if (!pointCount.ContainsKey(pointA))
            {
                topRight = pointA;
            }
            else if (!pointCount.ContainsKey(pointB))
            {
                topRight = pointB;
            }
            else if (!pointCount.ContainsKey(pointC))
            {
                topRight = pointC;
            }
            else
            {
                topRight = pointD;
            }

            // Next determine the dimension by tracing along the top or right side and counting black/white
            // transitions. Since we start inside a black module, we should see a number of transitions
            // equal to 1 less than the code dimension. Well, actually 2 less, because we are going to
            // end on a black module:

            // The top right point is actually the corner of a module, which is one of the two black modules
            // adjacent to the white module at the top right. Tracing to that corner from either the top left
            // or bottom right should work here. The number of transitions could be higher than it should be
            // due to noise. So we try both and take the min.

            int dimension = System.Math.Min(transitionsBetween(topLeft, topRight).Transitions, transitionsBetween(bottomRight, topRight).Transitions);

            if ((dimension & 0x01) == 1)
            {
                // it can't be odd, so, round... up?
                dimension++;
            }
            dimension += 2;

            BitMatrix bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, dimension);

            return(new DetectorResult(bits, new ResultPoint[] { pointA, pointB, pointC, pointD }));
        }
예제 #2
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; // black
                stateCount[1] = 0; // white
                stateCount[2] = 0; // black
                stateCount[3] = 0; // white
                stateCount[4] = 0; // black
                int currentState = 0;
                for (int j = 0; j < maxJ; j++)
                {
                    if (image.get_Renamed(j, i))
                    {
                        // Black pixel
                        if ((currentState % 2) == 1)                         // if currentState is White (i.e. 1 or 3)
                        {
                            // Counting white pixels
                            currentState++;
                        }
                        stateCount[currentState]++;
                    }
                    else
                    {
                        // White pixel
                        if ((currentState % 2) == 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();
            if (patternInfo.IsBlank())
            {
                return(null);
            }

            ResultPoint.orderBestPatterns(patternInfo);

            return(new FinderPatternInfo(patternInfo));
        }
예제 #3
0
        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);
            }
        }