private FormatInformation(int formatInfo) { // Bits 3,4 m_ErrorCorrectionLevelInternal = ErrorCorrectionLevelInternal.forBits((formatInfo >> 3) & 0x03); // Bottom 3 bits dataMask = (sbyte)(formatInfo & 0x07); }
private FormatInformation(int formatInfo) { // Bits 3,4 m_ErrorCorrectionLevelInternal = ErrorCorrectionLevelInternal.forBits((formatInfo >> 3) & 0x03); // Bottom 3 bits dataMask = (sbyte) (formatInfo & 0x07); }
internal static void encode(System.String content, ErrorCorrectionLevelInternal m_EcLevelInternal, System.Collections.Hashtable hints, QRCodeInternal qrCodeInternal) { System.String encoding = hints == null?null:(System.String) hints[EncodeHintType.CHARACTER_SET]; if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } // Step 1: Choose the mode (encoding). Mode mode = chooseMode(content, encoding); // Step 2: Append "bytes" into "dataBits" in appropriate encoding. BitVector dataBits = new BitVector(); appendBytes(content, mode, dataBits, encoding); // Step 3: Initialize QR code that can contain "dataBits". int numInputBytes = dataBits.sizeInBytes(); initQRCode(numInputBytes, m_EcLevelInternal, mode, qrCodeInternal); // Step 4: Build another bit vector that contains header and data. BitVector headerAndDataBits = new BitVector(); // Step 4.5: Append ECI message if applicable if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding)) { CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding); if (eci != null) { appendECI(eci, headerAndDataBits); } } appendModeInfo(mode, headerAndDataBits); int numLetters = mode.Equals(Mode.BYTE)?dataBits.sizeInBytes():content.Length; appendLengthInfo(numLetters, qrCodeInternal.Version, mode, headerAndDataBits); headerAndDataBits.appendBitVector(dataBits); // Step 5: Terminate the bits properly. terminateBits(qrCodeInternal.NumDataBytes, headerAndDataBits); // Step 6: Interleave data bits with error correction code. BitVector finalBits = new BitVector(); interleaveWithECBytes(headerAndDataBits, qrCodeInternal.NumTotalBytes, qrCodeInternal.NumDataBytes, qrCodeInternal.NumRSBlocks, finalBits); // Step 7: Choose the mask pattern and set to "QRCodeInternal". ByteMatrix matrix = new ByteMatrix(qrCodeInternal.MatrixWidth, qrCodeInternal.MatrixWidth); qrCodeInternal.MaskPattern = chooseMaskPattern(finalBits, qrCodeInternal.EcLevelInternal, qrCodeInternal.Version, matrix); // Step 8. Build the matrix and set it to "QRCodeInternal". MatrixUtil.buildMatrix(finalBits, qrCodeInternal.EcLevelInternal, qrCodeInternal.Version, qrCodeInternal.MaskPattern, matrix); qrCodeInternal.Matrix = matrix; // Step 9. Make sure we have a valid QR Code. if (!qrCodeInternal.Valid) { throw new WriterException("Invalid QR code: " + qrCodeInternal.ToString()); } }
// Build 2D matrix of QR Code from "dataBits" with "m_EcLevelInternal", "version" and "getMaskPattern". On // success, store the result in "matrix" and return true. internal static void buildMatrix(BitVector dataBits, ErrorCorrectionLevelInternal m_EcLevelInternal, int version, int maskPattern, ByteMatrix matrix) { clearMatrix(matrix); embedBasicPatterns(version, matrix); // Type information appear with any version. embedTypeInfo(m_EcLevelInternal, maskPattern, matrix); // Version info appear if version >= 7. maybeEmbedVersionInfo(version, matrix); // Data should be embedded at end. embedDataBits(dataBits, maskPattern, matrix); }
/// <summary> Encode "bytes" with the error correction level "m_EcLevelInternal". The encoding mode will be chosen /// internally by chooseMode(). On success, store the result in "QRCodeInternal". /// /// We recommend you to use QRCodeInternal.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. /// </summary> public static void encode(System.String content, ErrorCorrectionLevelInternal m_EcLevelInternal, QRCodeInternal qrCodeInternal) { encode(content, m_EcLevelInternal, null, qrCodeInternal); }
/// <summary> Initialize "QRCodeInternal" according to "numInputBytes", "m_EcLevelInternal", and "mode". On success, /// modify "QRCodeInternal". /// </summary> internal static void initQRCode(int numInputBytes, ErrorCorrectionLevelInternal m_EcLevelInternal, Mode mode, QRCodeInternal qrCodeInternal) { qrCodeInternal.EcLevelInternal = m_EcLevelInternal; qrCodeInternal.Mode = mode; // In the following comments, we use numbers of Version 7-H. for (int versionNum = 1; versionNum <= 40; versionNum++) { Version version = Version.getVersionForNumber(versionNum); // numBytes = 196 int numBytes = version.TotalCodewords; // getNumECBytes = 130 Version.ECBlocks ecBlocks = version.getECBlocksForLevel(m_EcLevelInternal); int numEcBytes = ecBlocks.TotalECCodewords; // getNumRSBlocks = 5 int numRSBlocks = ecBlocks.NumBlocks; // getNumDataBytes = 196 - 130 = 66 int numDataBytes = numBytes - numEcBytes; // We want to choose the smallest version which can contain data of "numInputBytes" + some // extra bits for the header (mode info and length info). The header can be three bytes // (precisely 4 + 16 bits) at most. Hence we do +3 here. if (numDataBytes >= numInputBytes + 3) { // Yay, we found the proper rs block info! qrCodeInternal.Version = versionNum; qrCodeInternal.NumTotalBytes = numBytes; qrCodeInternal.NumDataBytes = numDataBytes; qrCodeInternal.NumRSBlocks = numRSBlocks; // getNumECBytes = 196 - 66 = 130 qrCodeInternal.NumECBytes = numEcBytes; // matrix width = 21 + 6 * 4 = 45 qrCodeInternal.MatrixWidth = version.DimensionForVersion; return ; } } throw new WriterException("Cannot find proper rs block info (input data too big?)"); }
internal static int chooseMaskPattern(BitVector bits, ErrorCorrectionLevelInternal m_EcLevelInternal, int version, ByteMatrix matrix) { int minPenalty = System.Int32.MaxValue; // Lower penalty is better. int bestMaskPattern = - 1; // We try all mask patterns to choose the best one. for (int maskPattern = 0; maskPattern < QRCodeInternal.NUM_MASK_PATTERNS; maskPattern++) { MatrixUtil.buildMatrix(bits, m_EcLevelInternal, version, maskPattern, matrix); int penalty = calculateMaskPenalty(matrix); if (penalty < minPenalty) { minPenalty = penalty; bestMaskPattern = maskPattern; } } return bestMaskPattern; }
// Make bit vector of type information. On success, store the result in "bits" and return true. // Encode error correction level and mask pattern. See 8.9 of // JISX0510:2004 (p.45) for details. internal static void makeTypeInfoBits(ErrorCorrectionLevelInternal m_EcLevelInternal, int maskPattern, BitVector bits) { if (!QRCodeInternal.isValidMaskPattern(maskPattern)) { throw new WriterException("Invalid mask pattern"); } int typeInfo = (m_EcLevelInternal.Bits << 3) | maskPattern; bits.appendBits(typeInfo, 5); int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY); bits.appendBits(bchCode, 10); BitVector maskBits = new BitVector(); maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15); bits.xor(maskBits); if (bits.size() != 15) { // Just in case. throw new WriterException("should not happen but we got: " + bits.size()); } }
// Embed type information. On success, modify the matrix. internal static void embedTypeInfo(ErrorCorrectionLevelInternal m_EcLevelInternal, int maskPattern, ByteMatrix matrix) { BitVector typeInfoBits = new BitVector(); makeTypeInfoBits(m_EcLevelInternal, maskPattern, typeInfoBits); for (int i = 0; i < typeInfoBits.size(); ++i) { // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in // "typeInfoBits". int bit = typeInfoBits.at(typeInfoBits.size() - 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[y1, x1] = (sbyte)bit; if (i < 8) { // Right top corner. int x2 = matrix.Width - i - 1; int y2 = 8; matrix[y2, x2] = (sbyte)bit; } else { // Left bottom corner. int x2 = 8; int y2 = matrix.Height - 7 + (i - 8); matrix[y2, x2] = (sbyte)bit; } } }
internal static ErrorCorrectionLevel FromInternal(ErrorCorrectionLevelInternal level) { return (ErrorCorrectionLevel) level.ordinal(); }
public QRCodeInternal() { mode = null; m_EcLevelInternal = null; version = - 1; matrixWidth = - 1; maskPattern = - 1; numTotalBytes = - 1; numDataBytes = - 1; numECBytes = - 1; numRSBlocks = - 1; matrix = null; }