// Embed position adjustment patterns if need be. private static void maybeEmbedPositionAdjustmentPatterns(int version, ByteMatrix matrix) throws WriterException { if (version < 2) { // The patterns appear if version >= 2 return; } int index = version - 1; int[] coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index]; int numCoordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index].length; for (int i = 0; i < numCoordinates; ++i) { for (int j = 0; j < numCoordinates; ++j) { int y = coordinates[i]; int x = coordinates[j]; if (x == -1 || y == -1) { continue; } // If the cell is unset, we embed the position adjustment // pattern here. if (isEmpty(matrix.get(x, y))) { // -2 is necessary since the x/y coordinates point to the // center of the pattern, // not the // left top corner. embedPositionAdjustmentPattern(x - 2, y - 2, matrix); } } } }
public int at(int k, int l) { byte byte0 = j.get(k, l); if (byte0 != 0 && byte0 != 1) { throw new IllegalStateException("Bad value"); } else { return byte0; } }
private static void embedTimingPatterns(ByteMatrix matrix) throws WriterException { for (int i = 8; i < matrix.getWidth() - 8; ++i) { int bit = (i + 1) % 2; if (!isValidValue(matrix.get(i, 6))) { throw new WriterException(); } if (isEmpty(matrix.get(i, 6))) { matrix.set(i, 6, bit); } if (!isValidValue(matrix.get(6, i))) { throw new WriterException(); } if (isEmpty(matrix.get(6, i))) { matrix.set(6, i, bit); } } }
private static void embedTimingPatterns(ByteMatrix matrix) throws WriterException { // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical // separation patterns (size 1). Thus, 8 = 7 + 1. for (int i = 8; i < matrix.getWidth() - 8; ++i) { int bit = (i + 1) % 2; // Horizontal line. if (!isValidValue(matrix.get(i, 6))) { throw new WriterException(); } if (isEmpty(matrix.get(i, 6))) { matrix.set(i, 6, bit); } // Vertical line. if (!isValidValue(matrix.get(6, i))) { throw new WriterException(); } if (isEmpty(matrix.get(6, i))) { matrix.set(6, i, bit); } } }
private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { 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 { 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]); } }
// Embed "dataBits" using "getMaskPattern". On success, modify the matrix // and return true. // For debugging purposes, it skips masking process if "getMaskPattern" is // -1. // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits. static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix) throws WriterException { int bitIndex = 0; int direction = -1; // Start from the right bottom cell. int x = matrix.getWidth() - 1; int y = matrix.getHeight() - 1; while (x > 0) { // Skip the vertical timing pattern. if (x == 6) { x -= 1; } while (y >= 0 && y < matrix.getHeight()) { for (int i = 0; i < 2; ++i) { int xx = x - i; // Skip the cell if it's not empty. if (!isEmpty(matrix.get(xx, y))) { continue; } boolean bit; if (bitIndex < dataBits.getSize()) { bit = dataBits.get(bitIndex); ++bitIndex; } else { // Padding bit. If there is no bit left, we'll fill the // left cells with 0, // as described // in 8.4.9 of JISX0510:2004 (p. 24). bit = false; } // Skip masking if mask_pattern is -1. if (maskPattern != -1) { if (MaskUtil.getDataMaskBit(maskPattern, xx, y)) { bit = !bit; } } matrix.set(xx, y, bit); } y += direction; } direction = -direction; // Reverse the direction. y += direction; x -= 2; // Move to the left. } // All bits should be consumed. if (bitIndex != dataBits.getSize()) { throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.getSize()); } }
private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { 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 embedPositionAdjustmentPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException { if (POSITION_ADJUSTMENT_PATTERN[0].length != 5 || POSITION_ADJUSTMENT_PATTERN.length != 5) { throw new WriterException("Bad position adjustment"); } for (int y = 0; y < 5; ++y) { for (int x = 0; x < 5; ++x) { if (!isEmpty(matrix.get(xStart + x, yStart + y))) { throw new WriterException(); } matrix.set(xStart + x, yStart + y, POSITION_ADJUSTMENT_PATTERN[y][x]); } } }
static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix) throws WriterException { int bitIndex = 0; int direction = -1; int x = matrix.getWidth() - 1; int y = matrix.getHeight() - 1; while (x > 0) { if (x == 6) { x -= 1; } while (y >= 0 && y < matrix.getHeight()) { for (int i = 0; i < 2; ++i) { int xx = x - i; if (!isEmpty(matrix.get(xx, y))) { continue; } boolean bit; if (bitIndex < dataBits.getSize()) { bit = dataBits.get(bitIndex); ++bitIndex; } else { bit = false; } if (maskPattern != -1) { if (MaskUtil.getDataMaskBit(maskPattern, xx, y)) { bit = !bit; } } matrix.set(xx, y, bit); } y += direction; } direction = -direction; y += direction; x -= 2; } if (bitIndex != dataBits.getSize()) { throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.getSize()); } }
private static void maybeEmbedPositionAdjustmentPatterns(int version, ByteMatrix matrix) throws WriterException { if (version < 2) { return; } int index = version - 1; int[] coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index]; int numCoordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index].length; for (int i = 0; i < numCoordinates; ++i) { for (int j = 0; j < numCoordinates; ++j) { int y = coordinates[i]; int x = coordinates[j]; if (x == -1 || y == -1) { continue; } if (isEmpty(matrix.get(x, y))) { embedPositionAdjustmentPattern(x - 2, y - 2, matrix); } } } }
// Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap). 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; }
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); }