private static BitMatrix renderResult(QRCode code, 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(0, qrWidth); int outputHeight = Math.max(0, 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 boolean diagonalTrue(ByteMatrix byteMatrix, int x, int y) { return byteMatrix.get(x + 1, y + 1) == 1; } }
private static boolean rightTrue(ByteMatrix byteMatrix, int x, int y) { return x + 1 < byteMatrix.getWidth() && byteMatrix.get(x + 1, y) == 1; }
private static boolean belowTrue(ByteMatrix byteMatrix, int x, int y) { return y + 1 < byteMatrix.getHeight() && byteMatrix.get(x, y + 1) == 1; }
private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) throws WriterException { if (matrix.get(8, matrix.getHeight() - 8) == 0) { throw new WriterException(); } matrix.set(8, matrix.getHeight() - 8, 1); }
public int at(int x, int y) { // The value must be zero or one. int value = matrix.get(x, y); if (!(value == 0 || value == 1)) { // this is really like an assert... not sure what better exception to use? throw new RuntimeException("Bad value"); } return value; }
private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) throws WriterException { if (matrix.get(8, matrix.getHeight() - 8) == 0) { throw new WriterException(); } matrix.set(8, matrix.getHeight() - 8, 1); }
public int at(int x, int y) { // The value must be zero or one. int value = matrix.get(x, y); if (!(value == 0 || value == 1)) { // this is really like an assert... not sure what better exception to use? throw new RuntimeException("Bad value"); } return value; }
private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) throws WriterException { if (matrix.get(8, matrix.getHeight() - 8) == 0) { throw new WriterException(); } matrix.set(8, matrix.getHeight() - 8, 1); }
private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { for (int y = 0; y < 7; ++y) { if (!isEmpty(matrix.get(xStart, yStart + y))) { throw new WriterException(); } matrix.set(xStart, yStart + y, 0); } }
private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) throws WriterException { if (matrix.get(8, matrix.getHeight() - 8) == 0) { throw new WriterException(); } matrix.set(8, matrix.getHeight() - 8, 1); }
private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { for (int x = 0; x < 8; ++x) { if (!isEmpty(matrix.get(xStart + x, yStart))) { throw new WriterException(); } matrix.set(xStart + x, yStart, 0); } }
private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { for (int x = 0; x < 8; ++x) { if (!isEmpty(matrix.get(xStart + x, yStart))) { throw new WriterException(); } matrix.set(xStart + x, yStart, 0); } }
private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { for (int y = 0; y < 7; ++y) { if (!isEmpty(matrix.get(xStart, yStart + y))) { throw new WriterException(); } matrix.set(xStart, yStart + y, 0); } }
public static Bitmap generateQrCode(String myCodeText) throws WriterException { Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage QRCodeWriter qrCodeWriter = new QRCodeWriter(); int size = 256; ByteMatrix bitMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap); int width = bitMatrix.width(); Bitmap bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565); for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { bmp.setPixel(y, x, bitMatrix.get(x, y)==0 ? Color.BLACK : Color.WHITE); } } return bmp; }
private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { // We know the width and height. if (HORIZONTAL_SEPARATION_PATTERN[0].length != 8 || HORIZONTAL_SEPARATION_PATTERN.length != 1) { throw new WriterException("Bad horizontal separation pattern"); } for (int x = 0; x < 8; ++x) { if (!isEmpty(matrix.get(xStart + x, yStart))) { throw new WriterException(); } matrix.set(xStart + x, yStart, HORIZONTAL_SEPARATION_PATTERN[0][x]); } }
private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { // We know the width and height. if (VERTICAL_SEPARATION_PATTERN[0].length != 1 || VERTICAL_SEPARATION_PATTERN.length != 7) { throw new WriterException("Bad vertical separation pattern"); } for (int y = 0; y < 7; ++y) { if (!isEmpty(matrix.get(xStart, yStart + y))) { throw new WriterException(); } matrix.set(xStart, yStart + y, VERTICAL_SEPARATION_PATTERN[y][0]); } }
private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { // We know the width and height. if (HORIZONTAL_SEPARATION_PATTERN[0].length != 8 || HORIZONTAL_SEPARATION_PATTERN.length != 1) { throw new WriterException("Bad horizontal separation pattern"); } for (int x = 0; x < 8; ++x) { if (!isEmpty(matrix.get(xStart + x, yStart))) { throw new WriterException(); } matrix.set(xStart + x, yStart, HORIZONTAL_SEPARATION_PATTERN[0][x]); } }
private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { // We know the width and height. if (POSITION_DETECTION_PATTERN[0].length != 7 || POSITION_DETECTION_PATTERN.length != 7) { throw new WriterException("Bad position detection pattern"); } for (int y = 0; y < 7; ++y) { for (int x = 0; x < 7; ++x) { if (!isEmpty(matrix.get(xStart + x, yStart + y))) { throw new WriterException(); } matrix.set(xStart + x, yStart + y, POSITION_DETECTION_PATTERN[y][x]); } } }
private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { // We know the width and height. if (VERTICAL_SEPARATION_PATTERN[0].length != 1 || VERTICAL_SEPARATION_PATTERN.length != 7) { throw new WriterException("Bad vertical separation pattern"); } for (int y = 0; y < 7; ++y) { if (!isEmpty(matrix.get(xStart, yStart + y))) { throw new WriterException(); } matrix.set(xStart, yStart + y, VERTICAL_SEPARATION_PATTERN[y][0]); } }