private static void AppendECI(CharacterSetECI eci, BitVector bits) { bits.AppendBits(Mode.ECI.GetBits(), 4); // This is correct for values up to 127, which is all we need now. bits.AppendBits(eci.GetValue(), 8); }
/// <summary>Append length info.</summary> /// <remarks>Append length info. On success, store the result in "bits".</remarks> internal static void AppendLengthInfo(int numLetters, int version, Mode mode, BitVector bits) { int numBits = mode.GetCharacterCountBits(Version.GetVersionForNumber(version)); if (numLetters > ((1 << numBits) - 1)) { throw new WriterException(numLetters + "is bigger than" + ((1 << numBits) - 1)); } bits.AppendBits(numLetters, numBits); }
/// <summary>Interleave "bits" with corresponding error correction bytes.</summary> /// <remarks> /// Interleave "bits" with corresponding error correction bytes. On success, store the result in /// "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details. /// </remarks> internal static void InterleaveWithECBytes(BitVector bits, int numTotalBytes, int numDataBytes, int numRSBlocks , BitVector result) { // "bits" must have "getNumDataBytes" bytes of data. if (bits.SizeInBytes() != numDataBytes) { throw new WriterException("Number of bits and data bytes does not match"); } // Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll // store the divided data bytes blocks and error correction bytes blocks into "blocks". int dataBytesOffset = 0; int maxNumDataBytes = 0; int maxNumEcBytes = 0; // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number. IList <BlockPair> blocks = new List <BlockPair>(numRSBlocks); for (int i = 0; i < numRSBlocks; ++i) { int[] numDataBytesInBlock = new int[1]; int[] numEcBytesInBlock = new int[1]; GetNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes, numRSBlocks, i, numDataBytesInBlock, numEcBytesInBlock ); ByteArray dataBytes = new ByteArray(); dataBytes.Set(bits.GetArray(), dataBytesOffset, numDataBytesInBlock[0]); ByteArray ecBytes = GenerateECBytes(dataBytes, numEcBytesInBlock[0]); blocks.Add(new BlockPair(dataBytes, ecBytes)); maxNumDataBytes = Math.Max(maxNumDataBytes, dataBytes.Size()); maxNumEcBytes = Math.Max(maxNumEcBytes, ecBytes.Size()); dataBytesOffset += numDataBytesInBlock[0]; } if (numDataBytes != dataBytesOffset) { throw new WriterException("Data bytes does not match offset"); } // First, place data blocks. for (int i = 0; i < maxNumDataBytes; ++i) { for (int j = 0; j < blocks.Count; ++j) { ByteArray dataBytes = blocks[j].GetDataBytes(); if (i < dataBytes.Size()) { result.AppendBits(dataBytes.At(i), 8); } } } // Then, place error correction blocks. for (int i = 0; i < maxNumEcBytes; ++i) { for (int j = 0; j < blocks.Count; ++j) { ByteArray ecBytes = blocks[j].GetErrorCorrectionBytes(); if (i < ecBytes.Size()) { result.AppendBits(ecBytes.At(i), 8); } } } // Should be same. if (numTotalBytes != result.SizeInBytes()) { throw new WriterException("Interleaving error: " + numTotalBytes + " and " + result.SizeInBytes() + " differ." ); } }
/// <summary>Append mode info.</summary> /// <remarks>Append mode info. On success, store the result in "bits".</remarks> internal static void AppendModeInfo(Mode mode, BitVector bits) { bits.AppendBits(mode.GetBits(), 4); }
/// <summary>Encode "bytes" with the error correction level "ecLevel".</summary> /// <remarks> /// Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen /// internally by chooseMode(). On success, store the result in "qrCode". /// <para /> /// 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. /// <para /> /// 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. /// </remarks> /// <param name="content">String to encode</param> /// <param name="ecLevel">Error-correction level to use</param> /// <param name="hints">Optional Map containing encoding and suggested minimum version to use</param> /// <param name="qrCode">QR code to store the result in</param> public static void Encode(String content, ErrorCorrectionLevel ecLevel, IDictionary <EncodeHintType, Object > hints, QRCode qrCode) { String encoding = hints == null ? null : (String)hints.Get(EncodeHintType.CHARACTER_SET); if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } int desiredMinVersion = (hints == null || hints.Get(EncodeHintType.MIN_VERSION_NR) == null) ? 1 : (int)hints .Get(EncodeHintType.MIN_VERSION_NR); //Check if desired level is within bounds of [1,40] if (desiredMinVersion < 1) { desiredMinVersion = 1; } if (desiredMinVersion > 40) { desiredMinVersion = 40; } // 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, ecLevel, desiredMinVersion, mode, qrCode); // 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, qrCode.GetVersion(), mode, 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()); } }