/** * Convert the ByteMatrix to BitMatrix. * * @param matrix The input matrix. * @return The output matrix. */ private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) { int matrixWidgth = matrix.getWidth(); int matrixHeight = matrix.getHeight(); BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight); output.clear(); for (int i = 0; i < matrixWidgth; i++) { for (int j = 0; j < matrixHeight; j++) { // Zero is white in the bytematrix if (matrix.get(i, j) == 1) { output.set(i, j); } } } return output; }
/** * Convert the ByteMatrix to BitMatrix. * * @param matrix * The input matrix. * @return The output matrix. */ private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) { int matrixWidgth = matrix.getWidth(); int matrixHeight = matrix.getHeight(); BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight); output.clear(); for (int i = 0; i < matrixWidgth; i++) { for (int j = 0; j < matrixHeight; j++) { // Zero is white in the bytematrix if (matrix.get(i, j) == 1) { output.set(i, j); } } } return output; }
private void drawQRCodeRect(Canvas canvas, ByteMatrix input, Rect qrRegionRect, int multiple , int qrWidth, int qrHeight, Paint paint) { Rect qrBox = new Rect(); int r = (int) (multiple * 0.4); for (int inputY = 0, outputY = qrRegionRect.top; inputY < qrHeight; inputY++, outputY += multiple) { for (int inputX = 0, outputX = qrRegionRect.left; inputX < qrWidth; inputX++, outputX += multiple) { qrBox.left = outputX; qrBox.right = outputX + multiple; qrBox.top = outputY; qrBox.bottom = outputY + multiple; if (input.get(inputX, inputY) == 1) { // canvas.drawRect(new Rect(outputX, outputY, outputX + multiple, outputY + multiple), paint); drawRoundRect(canvas, new RectF(outputX, outputY, outputX + multiple, outputY + multiple), paint, r , (isSet(input, inputX - 1, inputY - 1) || isSet(input, inputX, inputY - 1) || isSet(input, inputX - 1, inputY)) , (isSet(input, inputX, inputY - 1) || isSet(input, inputX + 1, inputY - 1) || isSet(input, inputX + 1, inputY)) , (isSet(input, inputX, inputY + 1) || isSet(input, inputX, inputY - 1) || isSet(input, inputX - 1, inputY)) , (isSet(input, inputX + 1, inputY) || isSet(input, inputX + 1, inputY + 1) || isSet(input, inputX, inputY + 1))); } } } }
/** * Create a QR matrix and render it use given configs. * * @param contents Contents to encode. * @param size Width as well as the height of the output QR code, includes margin. * @param margin Margin to add around the QR code. * @param dataDotScale Scale the data blocks and makes them appear smaller. * @param colorDark Color of blocks. Will be OVERRIDE by autoColor. (BYTE_DTA, BYTE_POS, BYTE_AGN, BYTE_TMG) * @param colorLight Color of empty space. Will be OVERRIDE by autoColor. (BYTE_EPT) * @param backgroundImage The background image to embed in the QR code. If null, no background image will be embedded. * @param whiteMargin If true, background image will not be drawn on the margin area. * @param autoColor If true, colorDark will be set to the dominant color of background. * @param binarize If true, all images will be binarized while rendering. Default is false. * @param binarizeThreshold Threshold value used while binarizing. Default is 128. 0 < threshold < 255. * @param roundedDataDots If true, data blocks will appear as filled circles. Default is false. * @param logoImage The logo image which appears at the center of the QR code. Null to disable. * @param logoMargin The margin around the logo image. 0 to disable. * @param logoCornerRadius The radius of logo image's corners. 0 to disable. * @param logoScale Logo's size = logoScale * innerRenderSize * @return Bitmap of QR code * @throws IllegalArgumentException Refer to the messages below. */ private static Bitmap create(String contents, int size, int margin, float dataDotScale, int colorDark, int colorLight, Bitmap backgroundImage, boolean whiteMargin, boolean autoColor, boolean binarize, int binarizeThreshold, boolean roundedDataDots, Bitmap logoImage, int logoMargin, int logoCornerRadius, float logoScale) throws IllegalArgumentException { if (contents.isEmpty()) { throw new IllegalArgumentException("Error: contents is empty. (contents.isEmpty())"); } if (size < 0) { throw new IllegalArgumentException("Error: a negative size is given. (size < 0)"); } if (margin < 0) { throw new IllegalArgumentException("Error: a negative margin is given. (margin < 0)"); } if (size - 2 * margin <= 0) { throw new IllegalArgumentException("Error: there is no space left for the QRCode. (size - 2 * margin <= 0)"); } ByteMatrix byteMatrix = getBitMatrix(contents); if (size - 2 * margin < byteMatrix.getWidth()) { throw new IllegalArgumentException("Error: there is no space left for the QRCode. (size - 2 * margin < " + byteMatrix.getWidth() + ")"); } if (dataDotScale < 0 || dataDotScale > 1) { throw new IllegalArgumentException("Error: an illegal data dot scale is given. (dataDotScale < 0 || dataDotScale > 1)"); } return render(byteMatrix, size - 2 * margin, margin, dataDotScale, colorDark, colorLight, backgroundImage, whiteMargin, autoColor, binarize, binarizeThreshold, roundedDataDots, logoImage, logoMargin, logoCornerRadius, logoScale); }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone * 2); int qrHeight = inputHeight + (quietZone * 2); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); // Padding includes both the quiet zone and the extra white pixels to accommodate the requested // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will // handle all the padding from 100x100 (the actual QR) up to 200x160. 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) == 1) { output.setRegion(outputX, outputY, multiple, multiple); } } } return output; }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone << 1); int qrHeight = inputHeight + (quietZone << 1); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); 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) == (byte) 1) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; outputX += multiple; } inputY++; outputY += multiple; } return output; }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone * 2); int qrHeight = inputHeight + (quietZone * 2); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); 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) == (byte) 1) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; outputX += multiple; } inputY++; outputY += multiple; } return output; }
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) { int matrixWidgth = matrix.getWidth(); int matrixHeight = matrix.getHeight(); BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight); output.clear(); for (int i = 0; i < matrixWidgth; i++) { for (int j = 0; j < matrixHeight; j++) { if (matrix.get(i, j) == (byte) 1) { output.set(i, j); } } } return output; }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone << 1); int qrHeight = inputHeight + (quietZone << 1); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); // Padding includes both the quiet zone and the extra white pixels to accommodate the requested // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will // handle all the padding from 100x100 (the actual QR) up to 200x160. 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) == 1) { output.setRegion(outputX, outputY, multiple, multiple); } } } return output; }