📄 qrcodeimagereader.java
字号:
for (int ay = 0; ay < samplingGrid.getHeight(); ay++) { for (int ax = 0; ax < samplingGrid.getWidth();ax++) { canvas.drawLines(samplingGrid.getXLines(ax,ay), java.awt.Color.BLUE); canvas.drawLines(samplingGrid.getYLines(ax,ay), java.awt.Color.BLUE); } } return samplingGrid; }*/ /** * Generic Sampling grid method */ SamplingGrid getSamplingGrid(FinderPattern finderPattern, AlignmentPattern alignmentPattern) { Point centers[][] = alignmentPattern.getCenter(); int version = finderPattern.getVersion(); 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; ///??? //Point[] targetCenters; //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(); /// Housing to order baseLineX = new Line(); baseLineY = new Line(); axis.setModulePitch(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.getX()-logicalUpperLeftPoint.getX(); int logicalHeight=logicalLowerLeftPoint.getY()-logicalUpperLeftPoint.getY(); 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.setOrigin(gridLineX.getP1()); axis.setModulePitch(modulePitch.top); gridLineX.setP1(axis.translate(i,0)); axis.setOrigin(gridLineX.getP2()); axis.setModulePitch(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.setOrigin(gridLineY.getP1()); axis.setModulePitch(modulePitch.left); gridLineY.setP1(axis.translate(0,i)); axis.setOrigin(gridLineY.getP2()); axis.setModulePitch(modulePitch.right); gridLineY.setP2(axis.translate(0,i)); samplingGrid.setYLine(ax,ay,i,gridLineY); } } } return samplingGrid; } //get module pitch in single area int getAreaModulePitch(Point start, Point end, int logicalDistance) { Line tempLine; tempLine = new Line(start, end); int realDistance = tempLine.getLength(); int modulePitch = (realDistance << DECIMAL_POINT) / logicalDistance; return modulePitch; } //gridLines[areaX][areaY][direction(x=0,y=1)][EachLines] boolean[][] getQRCodeMatrix(boolean[][] image, SamplingGrid gridLines) throws ArrayIndexOutOfBoundsException { //int gridSize = gridLines.getWidth() * gridLines.getWidth(0,0); int gridSize = gridLines.getTotalWidth();// now this is done within the SamplingGrid class...// if (gridLines.getWidth() >= 2)// gridSize-=1; canvas.println("gridSize="+gridSize); //canvas.println("gridLines.getWidth() * gridLines.getWidth(0,0) = "+gridLines.getWidth() * gridLines.getWidth(0,0)); Point bottomRightPoint = null; boolean[][] sampledMatrix = new boolean[gridSize][gridSize]; for (int ay = 0; ay < gridLines.getHeight(); ay++) { for (int ax = 0; ax < gridLines.getWidth(); ax++) { Vector sampledPoints = new Vector(); //only for visualize; for (int y = 0; y < gridLines.getHeight(ax,ay); y++) { for (int x = 0; x < gridLines.getWidth(ax,ay); x++) { int x1 = gridLines.getXLine(ax,ay,x).getP1().getX(); int y1 = gridLines.getXLine(ax,ay,x).getP1().getY(); int x2 = gridLines.getXLine(ax,ay,x).getP2().getX(); int y2 = gridLines.getXLine(ax,ay,x).getP2().getY(); int x3 = gridLines.getYLine(ax,ay,y).getP1().getX(); int y3 = gridLines.getYLine(ax,ay,y).getP1().getY(); int x4 = gridLines.getYLine(ax,ay,y).getP2().getX(); int y4 = gridLines.getYLine(ax,ay,y).getP2().getY(); int e = (y2 - y1) * (x3 - x4) - (y4 - y3) * (x1 - x2); int f = (x1 * y2 - x2 * y1) * (x3 - x4) - (x3 * y4 - x4 * y3) * (x1 - x2); int g = (x3 * y4 - x4 * y3) * (y2 - y1) - (x1 * y2 - x2 * y1) * (y4 - y3); sampledMatrix[gridLines.getX(ax, x)][gridLines.getY(ay, y)] = image[f / e][g / e]; if ((ay == gridLines.getHeight() -1 && ax == gridLines.getWidth() - 1) && y == gridLines.getHeight(ax, ay) - 1 && x == gridLines.getWidth(ax, ay) -1) bottomRightPoint = new Point(f / e,g / e); //calling canvas.drawPoint in loop can be very slow. // use canvas.drawPoints if you need //canvas.drawPoint(new Point(f / e,g / e), Color.RED); } } } } if (bottomRightPoint != null && (bottomRightPoint.getX() > image.length - 1 || bottomRightPoint.getY() > image[0].length - 1)) throw new ArrayIndexOutOfBoundsException("Sampling grid pointed out of image"); canvas.drawPoint(bottomRightPoint, Color.BLUE); return sampledMatrix; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -