public static int applyMaskPenaltyRule2(ByteMatrix matrix) { int penalty = 0; byte[][] array = matrix.getArray(); int width = matrix.getWidth(); int height = matrix.getHeight(); for (int y = 0; y < height - 1; ++y) { for (int x = 0; x < width - 1; ++x) { int value = array[y][x]; if (value == array[y][x + 1] && value == array[y + 1][x] && value == array[y + 1][x + 1]) { penalty += 3; } } } return penalty; }
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 void embedTimingPatterns(ByteMatrix matrix) { // -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 (isEmpty(matrix.get(i, 6))) { matrix.set(i, 6, bit); } // Vertical line. if (isEmpty(matrix.get(6, i))) { matrix.set(6, i, bit); } } }
public boolean isValid() { return // First check if all version are not uninitialized. mode != null && ecLevel != null && version != -1 && matrixWidth != -1 && maskPattern != -1 && numTotalBytes != -1 && numDataBytes != -1 && numECBytes != -1 && numRSBlocks != -1 && // Then check them in other ways.. isValidMaskPattern(maskPattern) && numTotalBytes == numDataBytes + numECBytes && // ByteMatrix stuff. matrix != null && matrixWidth == matrix.getWidth() && // See 7.3.1 of JISX0510:2004 (p.5). matrix.getWidth() == matrix.getHeight(); // Must be square. }
public static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) throws WriterException { BitArray typeInfoBits = new BitArray(); makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits); for (int i = 0; i < typeInfoBits.getSize(); ++i) { // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in // "typeInfoBits". boolean bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i); // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46). int x1 = TYPE_INFO_COORDINATES[i][0]; int y1 = TYPE_INFO_COORDINATES[i][1]; matrix.set(x1, y1, bit); if (i < 8) { // Right top corner. int x2 = matrix.getWidth() - i - 1; int y2 = 8; matrix.set(x2, y2, bit); } else { // Left bottom corner. int x2 = 8; int y2 = matrix.getHeight() - 7 + (i - 8); matrix.set(x2, y2, bit); } } }
int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = multiple * inputWidth + (QUIET_ZONE_SIZE << 1); int qrHeight = multiple * inputHeight + (QUIET_ZONE_SIZE << 1); ByteMatrix output = new ByteMatrix(qrWidth, qrHeight); byte[][] outputArray = output.getArray(); byte[][] inputArray = input.getArray(); for (int y = 0; y < inputHeight; y++) {
public static void maybeEmbedVersionInfo(int version, ByteMatrix matrix) throws WriterException { if (version < 7) { // Version info is necessary if version >= 7. return; // Don't need version info. } BitArray versionInfoBits = new BitArray(); makeVersionInfoBits(version, versionInfoBits); int bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0. for (int i = 0; i < 6; ++i) { for (int j = 0; j < 3; ++j) { // Place bits in LSB (least significant bit) to MSB order. boolean bit = versionInfoBits.get(bitIndex); bitIndex--; // Left bottom corner. matrix.set(i, matrix.getHeight() - 11 + j, bit); // Right bottom corner. matrix.set(matrix.getHeight() - 11 + j, i, bit); } } }
private static boolean diagonalTrue(ByteMatrix byteMatrix, int x, int y) { return byteMatrix.get(x + 1, y + 1) == 1; } }
try { QRCode code = Encoder.encode(content, ERROR_CORRECTION_LEVEL, null); int size = code.getMatrix().getWidth();
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 embedPositionAdjustmentPattern(int xStart, int yStart, ByteMatrix matrix) { for (int y = 0; y < 5; ++y) { int[] patternY = POSITION_ADJUSTMENT_PATTERN[y]; for (int x = 0; x < 5; ++x) { matrix.set(xStart + x, yStart + y, patternY[x]); } } }
ByteMatrix matrix = new ByteMatrix(dimension, dimension); int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix); qrCode.setMaskPattern(maskPattern);
static void clearMatrix(ByteMatrix matrix) { matrix.clear((byte) -1); }
public boolean isValid() { return // First check if all version are not uninitialized. mode != null && ecLevel != null && version != -1 && matrixWidth != -1 && maskPattern != -1 && numTotalBytes != -1 && numDataBytes != -1 && numECBytes != -1 && numRSBlocks != -1 && // Then check them in other ways.. isValidMaskPattern(maskPattern) && numTotalBytes == numDataBytes + numECBytes && // ByteMatrix stuff. matrix != null && matrixWidth == matrix.getWidth() && // See 7.3.1 of JISX0510:2004 (p.5). matrix.getWidth() == matrix.getHeight(); // Must be square. }