/// <summary> Generic Sampling grid method</summary>
        internal virtual SamplingGrid getSamplingGrid(FinderPattern finderPattern, AlignmentPattern alignmentPattern)
        {
            Point[][] centers     = alignmentPattern.getCenter();
            int       version     = finderPattern.Version;
            int       sqrtCenters = (version / 7) + 2;

            centers[0][0] = finderPattern.getCenter(FinderPattern.UL);
            centers[sqrtCenters - 1][0] = finderPattern.getCenter(FinderPattern.UR);
            centers[0][sqrtCenters - 1] = finderPattern.getCenter(FinderPattern.DL);
            //int sqrtNumModules = finderPattern.getSqrtNumModules(); /// The number of modules per one side is obtained
            int          sqrtNumArea = sqrtCenters - 1;
            SamplingGrid samplingGrid = new SamplingGrid(sqrtNumArea);
            Line         baseLineX, baseLineY, gridLineX, gridLineY;
            //int logicalDistance = alignmentPattern.getLogicalDistance();
            Axis        axis = new Axis(finderPattern.getAngle(), finderPattern.getModuleSize());
            ModulePitch modulePitch;

            // for each area :
            for (int ay = 0; ay < sqrtNumArea; ay++)
            {
                for (int ax = 0; ax < sqrtNumArea; ax++)
                {
                    modulePitch      = new ModulePitch(this);                /// Housing to order
                    baseLineX        = new Line();
                    baseLineY        = new Line();
                    axis.ModulePitch = finderPattern.getModuleSize();
                    Point[][] logicalCenters = AlignmentPattern.getLogicalCenter(finderPattern);

                    Point upperLeftPoint         = centers[ax][ay];
                    Point upperRightPoint        = centers[ax + 1][ay];
                    Point lowerLeftPoint         = centers[ax][ay + 1];
                    Point lowerRightPoint        = centers[ax + 1][ay + 1];
                    Point logicalUpperLeftPoint  = logicalCenters[ax][ay];
                    Point logicalUpperRightPoint = logicalCenters[ax + 1][ay];
                    Point logicalLowerLeftPoint  = logicalCenters[ax][ay + 1];
                    Point logicalLowerRightPoint = logicalCenters[ax + 1][ay + 1];

                    if (ax == 0 && ay == 0)                    // left upper corner
                    {
                        if (sqrtNumArea == 1)
                        {
                            upperLeftPoint  = axis.translate(upperLeftPoint, -3, -3);
                            upperRightPoint = axis.translate(upperRightPoint, 3, -3);
                            lowerLeftPoint  = axis.translate(lowerLeftPoint, -3, 3);
                            lowerRightPoint = axis.translate(lowerRightPoint, 6, 6);

                            logicalUpperLeftPoint.translate(-6, -6);
                            logicalUpperRightPoint.translate(3, -3);
                            logicalLowerLeftPoint.translate(-3, 3);
                            logicalLowerRightPoint.translate(6, 6);
                        }
                        else
                        {
                            upperLeftPoint  = axis.translate(upperLeftPoint, -3, -3);
                            upperRightPoint = axis.translate(upperRightPoint, 0, -6);
                            lowerLeftPoint  = axis.translate(lowerLeftPoint, -6, 0);

                            logicalUpperLeftPoint.translate(-6, -6);
                            logicalUpperRightPoint.translate(0, -6);
                            logicalLowerLeftPoint.translate(-6, 0);
                        }
                    }
                    else if (ax == 0 && ay == sqrtNumArea - 1)                    // left bottom corner
                    {
                        upperLeftPoint  = axis.translate(upperLeftPoint, -6, 0);
                        lowerLeftPoint  = axis.translate(lowerLeftPoint, -3, 3);
                        lowerRightPoint = axis.translate(lowerRightPoint, 0, 6);
                        logicalUpperLeftPoint.translate(-6, 0);
                        logicalLowerLeftPoint.translate(-6, 6);
                        logicalLowerRightPoint.translate(0, 6);
                    }
                    else if (ax == sqrtNumArea - 1 && ay == 0)                    // right upper corner
                    {
                        upperLeftPoint  = axis.translate(upperLeftPoint, 0, -6);
                        upperRightPoint = axis.translate(upperRightPoint, 3, -3);
                        lowerRightPoint = axis.translate(lowerRightPoint, 6, 0);
                        logicalUpperLeftPoint.translate(0, -6);
                        logicalUpperRightPoint.translate(6, -6);
                        logicalLowerRightPoint.translate(6, 0);
                    }
                    else if (ax == sqrtNumArea - 1 && ay == sqrtNumArea - 1)                    // right bottom corner
                    {
                        lowerLeftPoint  = axis.translate(lowerLeftPoint, 0, 6);
                        upperRightPoint = axis.translate(upperRightPoint, 6, 0);
                        lowerRightPoint = axis.translate(lowerRightPoint, 6, 6);
                        logicalLowerLeftPoint.translate(0, 6);
                        logicalUpperRightPoint.translate(6, 0);
                        logicalLowerRightPoint.translate(6, 6);
                    }
                    else if (ax == 0)                    // left side
                    {
                        upperLeftPoint = axis.translate(upperLeftPoint, -6, 0);
                        lowerLeftPoint = axis.translate(lowerLeftPoint, -6, 0);
                        logicalUpperLeftPoint.translate(-6, 0);
                        logicalLowerLeftPoint.translate(-6, 0);
                    }
                    else if (ax == sqrtNumArea - 1)                    // right
                    {
                        upperRightPoint = axis.translate(upperRightPoint, 6, 0);
                        lowerRightPoint = axis.translate(lowerRightPoint, 6, 0);
                        logicalUpperRightPoint.translate(6, 0);
                        logicalLowerRightPoint.translate(6, 0);
                    }
                    else if (ay == 0)                    // top
                    {
                        upperLeftPoint  = axis.translate(upperLeftPoint, 0, -6);
                        upperRightPoint = axis.translate(upperRightPoint, 0, -6);
                        logicalUpperLeftPoint.translate(0, -6);
                        logicalUpperRightPoint.translate(0, -6);
                    }
                    else if (ay == sqrtNumArea - 1)                    // bottom
                    {
                        lowerLeftPoint  = axis.translate(lowerLeftPoint, 0, 6);
                        lowerRightPoint = axis.translate(lowerRightPoint, 0, 6);
                        logicalLowerLeftPoint.translate(0, 6);
                        logicalLowerRightPoint.translate(0, 6);
                    }
                    if (ax == 0)
                    {
                        logicalUpperRightPoint.translate(1, 0);
                        logicalLowerRightPoint.translate(1, 0);
                    }
                    else
                    {
                        logicalUpperLeftPoint.translate(-1, 0);
                        logicalLowerLeftPoint.translate(-1, 0);
                    }
                    if (ay == 0)
                    {
                        logicalLowerLeftPoint.translate(0, 1);
                        logicalLowerRightPoint.translate(0, 1);
                    }
                    else
                    {
                        logicalUpperLeftPoint.translate(0, -1);
                        logicalUpperRightPoint.translate(0, -1);
                    }
                    int logicalWidth  = logicalUpperRightPoint.X - logicalUpperLeftPoint.X;
                    int logicalHeight = logicalLowerLeftPoint.Y - logicalUpperLeftPoint.Y;
                    if (version < 7)
                    {
                        logicalWidth  += 3;
                        logicalHeight += 3;
                    }
                    modulePitch.top    = getAreaModulePitch(upperLeftPoint, upperRightPoint, logicalWidth - 1);
                    modulePitch.left   = getAreaModulePitch(upperLeftPoint, lowerLeftPoint, logicalHeight - 1);
                    modulePitch.bottom = getAreaModulePitch(lowerLeftPoint, lowerRightPoint, logicalWidth - 1);
                    modulePitch.right  = getAreaModulePitch(upperRightPoint, lowerRightPoint, logicalHeight - 1);

                    baseLineX.setP1(upperLeftPoint);
                    baseLineY.setP1(upperLeftPoint);
                    baseLineX.setP2(lowerLeftPoint);
                    baseLineY.setP2(upperRightPoint);

                    samplingGrid.initGrid(ax, ay, logicalWidth, logicalHeight);
                    for (int i = 0; i < logicalWidth; i++)
                    {
                        gridLineX = new Line(baseLineX.getP1(), baseLineX.getP2());

                        axis.Origin      = gridLineX.getP1();
                        axis.ModulePitch = modulePitch.top;
                        gridLineX.setP1(axis.translate(i, 0));

                        axis.Origin      = gridLineX.getP2();
                        axis.ModulePitch = modulePitch.bottom;
                        gridLineX.setP2(axis.translate(i, 0));

                        samplingGrid.setXLine(ax, ay, i, gridLineX);
                    }
                    for (int i = 0; i < logicalHeight; i++)
                    {
                        gridLineY        = new Line(baseLineY.getP1(), baseLineY.getP2());
                        axis.Origin      = gridLineY.getP1();
                        axis.ModulePitch = modulePitch.left;
                        gridLineY.setP1(axis.translate(0, i));
                        axis.Origin      = gridLineY.getP2();
                        axis.ModulePitch = modulePitch.right;
                        gridLineY.setP2(axis.translate(0, i));
                        samplingGrid.setYLine(ax, ay, i, gridLineY);
                    }
                }
            }
            return(samplingGrid);
        }
示例#2
0
        internal static Point[][] getCenter(bool[][] image, FinderPattern finderPattern, Point[][] logicalCenters)
        {
            int  moduleSize  = finderPattern.getModuleSize();
            Axis axis        = new Axis(finderPattern.getAngle(), moduleSize);
            int  sqrtCenters = logicalCenters.Length;

            Point[][] centers = new Point[sqrtCenters][];
            for (int i = 0; i < sqrtCenters; i++)
            {
                centers[i] = new Point[sqrtCenters];
            }
            axis.Origin   = finderPattern.getCenter(FinderPattern.UL);
            centers[0][0] = axis.translate(3, 3);
            canvas.drawCross(centers[0][0], Color_Fields.BLUE);

            axis.Origin = finderPattern.getCenter(FinderPattern.UR);
            centers[sqrtCenters - 1][0] = axis.translate(-3, 3);
            canvas.drawCross(centers[sqrtCenters - 1][0], Color_Fields.BLUE);

            axis.Origin = finderPattern.getCenter(FinderPattern.DL);
            centers[0][sqrtCenters - 1] = axis.translate(3, -3);
            canvas.drawCross(centers[0][sqrtCenters - 1], Color_Fields.BLUE);

            Point tmpPoint = centers[0][0];

            for (int y = 0; y < sqrtCenters; y++)
            {
                for (int x = 0; x < sqrtCenters; x++)
                {
                    if ((x == 0 && y == 0) || (x == 0 && y == sqrtCenters - 1) || (x == sqrtCenters - 1 && y == 0))
                    {
                        continue;
                    }
                    Point target = null;
                    if (y == 0)
                    {
                        if (x > 0 && x < sqrtCenters - 1)
                        {
                            target = axis.translate(centers[x - 1][y], logicalCenters[x][y].X - logicalCenters[x - 1][y].X, 0);
                        }
                        centers[x][y] = new Point(target.X, target.Y);
                        canvas.drawCross(centers[x][y], Color_Fields.RED);
                    }
                    else if (x == 0)
                    {
                        if (y > 0 && y < sqrtCenters - 1)
                        {
                            target = axis.translate(centers[x][y - 1], 0, logicalCenters[x][y].Y - logicalCenters[x][y - 1].Y);
                        }
                        centers[x][y] = new Point(target.X, target.Y);
                        canvas.drawCross(centers[x][y], Color_Fields.RED);
                    }
                    else
                    {
                        Point t1 = axis.translate(centers[x - 1][y], logicalCenters[x][y].X - logicalCenters[x - 1][y].X, 0);
                        Point t2 = axis.translate(centers[x][y - 1], 0, logicalCenters[x][y].Y - logicalCenters[x][y - 1].Y);
                        centers[x][y] = new Point((t1.X + t2.X) / 2, (t1.Y + t2.Y) / 2 + 1);
                    }
                    if (finderPattern.Version > 1)
                    {
                        Point precisionCenter = getPrecisionCenter(image, centers[x][y]);
                        if (centers[x][y].distanceOf(precisionCenter) < 6)
                        {
                            canvas.drawCross(centers[x][y], Color_Fields.RED);
                            int dx = precisionCenter.X - centers[x][y].X;
                            int dy = precisionCenter.Y - centers[x][y].Y;
                            canvas.println("Adjust AP(" + x + "," + y + ") to d(" + dx + "," + dy + ")");
                            centers[x][y] = precisionCenter;
                        }
                    }
                    canvas.drawCross(centers[x][y], Color_Fields.BLUE);
                    canvas.drawLine(new Line(tmpPoint, centers[x][y]), Color_Fields.LIGHTBLUE);
                    tmpPoint = centers[x][y];
                }
            }
            return(centers);
        }
        public virtual QRCodeSymbol getQRCodeSymbol(int[][] image)
        {
            int longSide = (image.Length < image[0].Length)?image[0].Length:image.Length;

            DECIMAL_POINT = 23 - QRCodeUtility.sqrt(longSide / 256);
            bitmap        = filterImage(image);
            canvas.println("Drawing matrix.");
            canvas.drawMatrix(bitmap);
            canvas.println("Scanning Finder Pattern.");
            FinderPattern finderPattern = null;

            try
            {
                finderPattern = FinderPattern.findFinderPattern(bitmap);
            }
            catch (FinderPatternNotFoundException e)
            {
                canvas.println("Not found, now retrying...");
                bitmap = applyCrossMaskingMedianFilter(bitmap, 5);
                canvas.drawMatrix(bitmap);
                for (int i = 0; i < 1000000000; i++)
                {
                    ;
                }
                try
                {
                    finderPattern = FinderPattern.findFinderPattern(bitmap);
                }
                catch (FinderPatternNotFoundException e2)
                {
                    throw new SymbolNotFoundException(e2.Message);
                }
                catch (VersionInformationException e2)
                {
                    throw new SymbolNotFoundException(e2.Message);
                }
            }
            catch (VersionInformationException e)
            {
                throw new SymbolNotFoundException(e.Message);
            }
            canvas.println("FinderPattern at");
            String finderPatternCoordinates = finderPattern.getCenter(FinderPattern.UL).ToString() + finderPattern.getCenter(FinderPattern.UR).ToString() + finderPattern.getCenter(FinderPattern.DL).ToString();

            canvas.println(finderPatternCoordinates);
            int[] sincos = finderPattern.getAngle();
            canvas.println("Angle*4098: Sin " + System.Convert.ToString(sincos[0]) + "  " + "Cos " + System.Convert.ToString(sincos[1]));

            int version = finderPattern.Version;

            canvas.println("Version: " + System.Convert.ToString(version));
            if (version < 1 || version > 40)
            {
                throw new InvalidVersionException("Invalid version: " + version);
            }

            AlignmentPattern alignmentPattern = null;

            try
            {
                alignmentPattern = AlignmentPattern.findAlignmentPattern(bitmap, finderPattern);
            }
            catch (AlignmentPatternNotFoundException e)
            {
                throw new SymbolNotFoundException(e.Message);
            }

            int matrixLength = alignmentPattern.getCenter().Length;

            canvas.println("AlignmentPatterns at");
            for (int y = 0; y < matrixLength; y++)
            {
                String alignmentPatternCoordinates = "";
                for (int x = 0; x < matrixLength; x++)
                {
                    alignmentPatternCoordinates += alignmentPattern.getCenter()[x][y].ToString();
                }
                canvas.println(alignmentPatternCoordinates);
            }
            //for(int i = 0; i < 500000; i++) Console.out.println("");

            canvas.println("Creating sampling grid.");
            //[TODO] need all-purpose method
            //samplingGrid = getSamplingGrid2_6(finderPattern, alignmentPattern);
            samplingGrid = getSamplingGrid(finderPattern, alignmentPattern);
            canvas.println("Reading grid.");
            bool[][] qRCodeMatrix = null;
            try
            {
                qRCodeMatrix = getQRCodeMatrix(bitmap, samplingGrid);
            }
            catch (System.IndexOutOfRangeException e)
            {
                throw new SymbolNotFoundException("Sampling grid exceeded image boundary");
            }
            //canvas.drawMatrix(qRCodeMatrix);
            return(new QRCodeSymbol(qRCodeMatrix));
        }