private static BitMatrix renderResult(AztecCode code, int width, int height) { BitMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int outputWidth = Math.max(width, inputWidth); int outputHeight = Math.max(height, inputHeight); int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight); int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) { // Write the contents of this row of the barcode for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) { if (input.get(inputX, inputY)) { output.setRegion(outputX, outputY, multiple, multiple); } } } return output; }
private static BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Charset charset, int eccPercent, int layers) { if (format != BarcodeFormat.AZTEC) { throw new IllegalArgumentException("Can only encode AZTEC, but got " + format); } AztecCode aztec = Encoder.encode(contents.getBytes(charset), eccPercent, layers); return renderResult(aztec, width, height); }
private static BitMatrix renderResult(AztecCode code, int width, int height) { BitMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int outputWidth = Math.max(width, inputWidth); int outputHeight = Math.max(height, inputHeight); int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight); int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); int inputY = 0; int outputY = topPadding; while (inputY < inputHeight) { int inputX = 0; int outputX = leftPadding; while (inputX < inputWidth) { if (input.get(inputX, inputY)) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; outputX += multiple; } inputY++; outputY += multiple; } return output; }
private static BitMatrix encode(String contents, BarcodeFormat format, Charset charset, int eccPercent) { if (format != BarcodeFormat.AZTEC) { throw new IllegalArgumentException("Can only encode AZTEC, but got " + format); } AztecCode aztec = Encoder.encode(contents.getBytes(charset), eccPercent); return aztec.getMatrix(); }
private static List<Point> getOrientationPoints(AztecCode code) { int center = code.getMatrix().getWidth() / 2; int offset = code.isCompact() ? 5 : 7; List<Point> result = new ArrayList<>(); for (int xSign = -1; xSign <= 1; xSign += 2) { for (int ySign = -1; ySign <= 1; ySign += 2) { result.add(new Point(center + xSign * offset, center + ySign * offset)); result.add(new Point(center + xSign * (offset - 1), center + ySign * offset)); result.add(new Point(center + xSign * offset, center + ySign * (offset - 1))); } } return result; }