use of jp.sourceforge.qrcode.exception.AlignmentPatternNotFoundException in project qrcode by yanbe.
the class QRCodeImageReader method getQRCodeSymbol.
public QRCodeSymbol getQRCodeSymbol(int[][] image) throws SymbolNotFoundException {
int longSide = (image.length < image[0].length) ? image[0].length : image.length;
QRCodeImageReader.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);
try {
finderPattern = FinderPattern.findFinderPattern(bitmap);
} catch (FinderPatternNotFoundException e2) {
throw new SymbolNotFoundException(e2.getMessage());
} catch (VersionInformationException e2) {
throw new SymbolNotFoundException(e2.getMessage());
}
} catch (VersionInformationException e) {
throw new SymbolNotFoundException(e.getMessage());
}
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 " + Integer.toString(sincos[0]) + " " + "Cos " + Integer.toString(sincos[1]));
int version = finderPattern.getVersion();
canvas.println("Version: " + Integer.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.getMessage());
}
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++) System.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.");
boolean[][] qRCodeMatrix = null;
try {
qRCodeMatrix = getQRCodeMatrix(bitmap, samplingGrid);
} catch (ArrayIndexOutOfBoundsException e) {
throw new SymbolNotFoundException("Sampling grid exceeded image boundary");
}
//canvas.drawMatrix(qRCodeMatrix);
return new QRCodeSymbol(qRCodeMatrix);
}
use of jp.sourceforge.qrcode.exception.AlignmentPatternNotFoundException in project qrcode by yanbe.
the class AlignmentPattern method getPrecisionCenter.
/*
static Point[][] getCenter(boolean[][] image, FinderPattern finderPattern, Point[][] logicalCenters)
throws AlignmentPatternEdgeNotFoundException {
int moduleSize = finderPattern.getModuleSize();
int sin = finderPattern.getAngle()[0];
int cos = finderPattern.getAngle()[1];
Axis axis = new Axis(sin, cos, moduleSize);
int sqrtCenters = logicalCenters.length;
Point[][] centers = new Point[sqrtCenters][sqrtCenters];
axis.setOrigin(finderPattern.getCenter(FinderPattern.UL));
centers[0][0] = axis.translate(3, 3);
//centers[0][0] = finderPattern.getCenter(FinderPattern.UL);
axis.setOrigin(finderPattern.getCenter(FinderPattern.UR));
centers[sqrtCenters - 1][0] = axis.translate(-3, 3);
//centers[sqrtCenters - 1][0] = finderPattern.getCenter(FinderPattern.UR);
axis.setOrigin(finderPattern.getCenter(FinderPattern.DL));
centers[0][sqrtCenters - 1] = axis.translate(3, -3);
//centers[0][sqrtCenters - 1] = finderPattern.getCenter(FinderPattern.DL);
for (int y = 0; y < sqrtCenters; y++) {
for (int x = 0; x < sqrtCenters; x++) {
if (x == 1 && y == 0 && sqrtCenters == 3) { //型番7〜13の中央上の位置合せパターン
centers[x][y] = Point.getCenter(centers[0][0], centers[sqrtCenters - 1][0]);
}
else if (x == 0 && y == 1 && sqrtCenters == 3) {//型番7〜13の左中央の位置合せパターン
centers[x][y] = Point.getCenter(centers[0][0], centers[0][sqrtCenters - 1]);
}
else if (x >= 1 && y >= 1){
Line[] additionalLines = {
new Line(centers[x - 1][y - 1], centers[x][y - 1]),
new Line(centers[x - 1][y - 1], centers[x - 1][y])};
int dx = centers[x - 1][y].getX() - centers[x - 1][y - 1].getX();
int dy = centers[x - 1][y].getY() - centers[x - 1][y - 1].getY();
additionalLines[0].translate(dx,dy);
dx = centers[x][y - 1].getX() - centers[x - 1][y - 1].getX();
dy = centers[x][y - 1].getY() - centers[x - 1][y - 1].getY();
additionalLines[1].translate(dx,dy);
centers[x][y] = Point.getCenter(additionalLines[0].getP2(), additionalLines[1].getP2());
}
else // dummy alignment pattern (source is finder pattern)
continue;
try {
centers[x][y] = getPrecisionCenter(image, centers[x][y]);
} catch (AlignmentPatternEdgeNotFoundException e) {
e.printStackTrace();
throw e;
}
canvas.drawCross(centers[x][y], java.awt.Color.RED);
}
//System.out.println("");
}
return centers;
}
*/
static Point getPrecisionCenter(boolean[][] image, Point targetPoint) throws AlignmentPatternNotFoundException {
// find nearest dark point and update it as new rough center point
// when original rough center points light point
int tx = targetPoint.getX(), ty = targetPoint.getY();
if ((tx < 0 || ty < 0) || (tx > image.length - 1 || ty > image[0].length - 1))
throw new AlignmentPatternNotFoundException("Alignment Pattern finder exceeded out of image");
if (image[targetPoint.getX()][targetPoint.getY()] == QRCodeImageReader.POINT_LIGHT) {
int scope = 0;
boolean found = false;
while (!found) {
scope++;
for (int dy = scope; dy > -scope; dy--) {
for (int dx = scope; dx > -scope; dx--) {
int x = targetPoint.getX() + dx;
int y = targetPoint.getY() + dy;
if ((x < 0 || y < 0) || (x > image.length - 1 || y > image[0].length - 1))
throw new AlignmentPatternNotFoundException("Alignment Pattern finder exceeded out of image");
if (image[x][y] == QRCodeImageReader.POINT_DARK) {
targetPoint = new Point(targetPoint.getX() + dx, targetPoint.getY() + dy);
canvas.drawPoint(targetPoint, Color.RED);
found = true;
}
}
}
}
}
int x, lx, rx, y, uy, dy;
x = lx = rx = targetPoint.getX();
y = uy = dy = targetPoint.getY();
// GuoQing Hu's FIX
while (lx >= 1 && !targetPointOnTheCorner(image, lx, y, lx - 1, y)) lx--;
while (rx < image.length - 1 && !targetPointOnTheCorner(image, rx, y, rx + 1, y)) rx++;
while (uy >= 1 && !targetPointOnTheCorner(image, x, uy, x, uy - 1)) uy--;
while (dy < image[0].length - 1 && !targetPointOnTheCorner(image, x, dy, x, dy + 1)) dy++;
return new Point((lx + rx + 1) / 2, (uy + dy + 1) / 2);
}
Aggregations