// Encode "bytes" with the error correction level "getECLevel". The encoding mode will be chosen // internally by chooseMode(). On success, store the result in "qrCode" and return true. // We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for // "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very // strong error correction for this purpose. // // Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode() // with which clients can specify the encoding mode. For now, we don't need the functionality. public static void encode(String content, ErrorCorrectionLevel ecLevel, QRCode qrCode) { // Step 1: Choose the mode (encoding). Mode mode = chooseMode(content); // Step 2: Append "bytes" into "dataBits" in appropriate encoding. BitVector dataBits = new BitVector(); appendBytes(content, mode, dataBits); // Step 3: Initialize QR code that can contain "dataBits". int numInputBytes = dataBits.sizeInBytes(); initQRCode(numInputBytes, ecLevel, mode, qrCode); // Step 4: Build another bit vector that contains header and data. BitVector headerAndDataBits = new BitVector(); appendModeInfo(qrCode.getMode(), headerAndDataBits); appendLengthInfo(content.Length, qrCode.getVersion(), qrCode.getMode(), headerAndDataBits); headerAndDataBits.appendBitVector(dataBits); // Step 5: Terminate the bits properly. terminateBits(qrCode.getNumDataBytes(), headerAndDataBits); // Step 6: Interleave data bits with error correction code. BitVector finalBits = new BitVector(); interleaveWithECBytes(headerAndDataBits, qrCode.getNumTotalBytes(), qrCode.getNumDataBytes(), qrCode.getNumRSBlocks(), finalBits); // Step 7: Choose the mask pattern and set to "qrCode". ByteMatrix matrix = new ByteMatrix(qrCode.getMatrixWidth(), qrCode.getMatrixWidth()); qrCode.setMaskPattern(chooseMaskPattern(finalBits, qrCode.getECLevel(), qrCode.getVersion(), matrix)); // Step 8. Build the matrix and set it to "qrCode". MatrixUtil.buildMatrix(finalBits, qrCode.getECLevel(), qrCode.getVersion(), qrCode.getMaskPattern(), matrix); qrCode.setMatrix(matrix); // Step 9. Make sure we have a valid QR Code. if (!qrCode.isValid()) { throw new WriterException("Invalid QR code: " + qrCode.toString()); } }