public static QREncoderMatrix Encode(string content, QRCorrectionLevel correctionLevel) { string encoding = DEFAULT_ENCODING; QRMode mode = chooseMode(content, encoding); BitList header = new BitList(); header.AppendBits(mode.ModeSignature, 4); BitList data = new BitList(); AppendBytes(content, mode, data); int provisionalBitsNeeded = header.Size + mode.GetVersionCharacterCount(QRVersion.GetVersionByNumber(1)) + data.Size; QRVersion provisionalVersion = chooseVersion(provisionalBitsNeeded, correctionLevel); int bitsNeeded = header.Size + mode.GetVersionCharacterCount(provisionalVersion) + data.Size; QRVersion version = chooseVersion(bitsNeeded, correctionLevel); BitList headerNData = new BitList(); headerNData.AppendBitList(header); int numLetters = mode == QRMode.BYTE ? data.ByteSize : content.Length; AppendLengthInfo(numLetters, version, mode, headerNData); headerNData.AppendBitList(data); QRVersion.CorrectionBlockSet correctionBlockSet = version.GetBlockSetByLevel(correctionLevel); int dataBytesQty = version.TotalCodewords - correctionBlockSet.TotalCodewords; WriteTerminationSection(dataBytesQty, headerNData); BitList finalBits = MixWithCorrectionBytes(headerNData, version.TotalCodewords, dataBytesQty, correctionBlockSet.TotalQty); int dimension = version.Dimension; QREncoderMatrix matrix = new QREncoderMatrix(dimension, content, correctionLevel, mode, version); int maskPattern = chooseMaskPattern(finalBits, correctionLevel, version, matrix); matrix.MaskPattern = maskPattern; matrix.FormMatrix(finalBits, correctionLevel, version, maskPattern); return(matrix); }
private static QRVersion chooseVersion(int numInputBits, QRCorrectionLevel ecLevel) { for (int versionNum = 1; versionNum <= 40; versionNum++) { QRVersion version = QRVersion.GetVersionByNumber(versionNum); int numBytes = version.TotalCodewords; QRVersion.CorrectionBlockSet correctionBlocks = version.GetBlockSetByLevel(ecLevel); int numEcBytes = correctionBlocks.TotalCodewords; int numDataBytes = numBytes - numEcBytes; int totalInputBytes = (numInputBits + 7) / 8; if (numDataBytes >= totalInputBytes) { return(version); } } throw new NFXException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".chooseVersion(data)"); }