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; }
QRCode qrCode; try { qrCode = Encoder.encode(contentsToEncode, level, hints); patternCenters = qrCode.getVersion().getAlignmentPatternCenters();
private static int calculateMaskPenalty(ByteMatrix matrix) { return MaskUtil.applyMaskPenaltyRule1(matrix) + MaskUtil.applyMaskPenaltyRule2(matrix) + MaskUtil.applyMaskPenaltyRule3(matrix) + MaskUtil.applyMaskPenaltyRule4(matrix); }
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); } } }
static void embedBasicPatterns(Version version, ByteMatrix matrix) throws WriterException { // Let's get started with embedding big squares at corners. embedPositionDetectionPatternsAndSeparators(matrix); // Then, embed the dark dot at the left bottom corner. embedDarkDotAtLeftBottomCorner(matrix); // Position adjustment patterns appear if version >= 2. maybeEmbedPositionAdjustmentPatterns(version, matrix); // Timing patterns should be embedded after position adj. patterns. embedTimingPatterns(matrix); }
public static void buildMatrix(BitArray dataBits, ErrorCorrectionLevel ecLevel, int version, int maskPattern, ByteMatrix matrix) throws WriterException { clearMatrix(matrix); embedBasicPatterns(version, matrix); // Type information appear with any version. embedTypeInfo(ecLevel, maskPattern, matrix); // Version info appear if version >= 7. maybeEmbedVersionInfo(version, matrix); // Data should be embedded at end. embedDataBits(dataBits, maskPattern, matrix); }
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]); } } }
/** * Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and * give penalty to them. Example: 00000 or 11111. */ static int applyMaskPenaltyRule1(ByteMatrix matrix) { return applyMaskPenaltyRule1Internal(matrix, true) + applyMaskPenaltyRule1Internal(matrix, false); }
public static int calculateBCHCode(int value, int poly) { // If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1 // from 13 to make it 12. int msbSetInPoly = findMSBSet(poly); value <<= msbSetInPoly - 1; // Do the division business using exclusive-or operations. while (findMSBSet(value) >= msbSetInPoly) { value ^= poly << (findMSBSet(value) - msbSetInPoly); } // Now the "value" is the remainder (i.e. the BCH code) return value; }
public static void embedBasicPatterns(int version, ByteMatrix matrix) throws WriterException { // Let's get started with embedding big squares at corners. embedPositionDetectionPatternsAndSeparators(matrix); // Then, embed the dark dot at the left bottom corner. embedDarkDotAtLeftBottomCorner(matrix); // Position adjustment patterns appear if version >= 2. maybeEmbedPositionAdjustmentPatterns(version, matrix); // Timing patterns should be embedded after position adj. patterns. embedTimingPatterns(matrix); }
private static int calculateMaskPenalty(ByteMatrix matrix) { return MaskUtil.applyMaskPenaltyRule1(matrix) + MaskUtil.applyMaskPenaltyRule2(matrix) + MaskUtil.applyMaskPenaltyRule3(matrix) + MaskUtil.applyMaskPenaltyRule4(matrix); }
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]); } } }
/** * Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and * give penalty to them. Example: 00000 or 11111. */ static int applyMaskPenaltyRule1(ByteMatrix matrix) { return applyMaskPenaltyRule1Internal(matrix, true) + applyMaskPenaltyRule1Internal(matrix, false); }
static void embedBasicPatterns(Version version, ByteMatrix matrix) throws WriterException { // Let's get started with embedding big squares at corners. embedPositionDetectionPatternsAndSeparators(matrix); // Then, embed the dark dot at the left bottom corner. embedDarkDotAtLeftBottomCorner(matrix); // Position adjustment patterns appear if version >= 2. maybeEmbedPositionAdjustmentPatterns(version, matrix); // Timing patterns should be embedded after position adj. patterns. embedTimingPatterns(matrix); }
private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix) { for (int y = 0; y < 7; ++y) { int[] patternY = POSITION_DETECTION_PATTERN[y]; for (int x = 0; x < 7; ++x) { matrix.set(xStart + x, yStart + y, patternY[x]); } } }
public static void embedBasicPatterns(int version, ByteMatrix matrix) throws WriterException { // Let's get started with embedding big squares at corners. embedPositionDetectionPatternsAndSeparators(matrix); // Then, embed the dark dot at the left bottom corner. embedDarkDotAtLeftBottomCorner(matrix); // Position adjustment patterns appear if version >= 2. maybeEmbedPositionAdjustmentPatterns(version, matrix); // Timing patterns should be embedded after position adj. patterns. embedTimingPatterns(matrix); }
private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix) { for (int y = 0; y < 7; ++y) { int[] patternY = POSITION_DETECTION_PATTERN[y]; for (int x = 0; x < 7; ++x) { matrix.set(xStart + x, yStart + y, patternY[x]); } } }