示例#1
0
        internal static EncodationStruct Encode(IEnumerable <byte> content, ErrorCorrectionLevel eclevel)
        {
            EncoderBase encoderBase = CreateEncoder(Mode.EightBitByte, QRCodeConstantVariable.DefaultEncoding);

            BitList encodeContent = new BitList(content);

            int encodeContentLength = encodeContent.Count;

            VersionControlStruct vcStruct =
                VersionControl.InitialSetup(encodeContentLength, Mode.EightBitByte, eclevel, QRCodeConstantVariable.DefaultEncoding);

            BitList dataCodewords = new BitList();

            //Eci header
            if (vcStruct.isContainECI && vcStruct.ECIHeader != null)
            {
                dataCodewords.Add(vcStruct.ECIHeader);
            }
            //Header
            dataCodewords.Add(encoderBase.GetModeIndicator());
            int numLetter = encodeContentLength >> 3;

            dataCodewords.Add(encoderBase.GetCharCountIndicator(numLetter, vcStruct.VersionDetail.Version));
            //Data
            dataCodewords.Add(encodeContent);
            //Terminator Padding
            dataCodewords.TerminateBites(dataCodewords.Count, vcStruct.VersionDetail.NumDataBytes);

            int dataCodewordsCount = dataCodewords.Count;

            if ((dataCodewordsCount & 0x7) != 0)
            {
                throw new ArgumentException("data codewords is not byte sized.");
            }
            else if (dataCodewordsCount >> 3 != vcStruct.VersionDetail.NumDataBytes)
            {
                throw new ArgumentException("datacodewords num of bytes not equal to NumDataBytes for current version");
            }

            EncodationStruct encStruct = new EncodationStruct(vcStruct);

            encStruct.Mode          = Mode.EightBitByte;
            encStruct.DataCodewords = dataCodewords;
            return(encStruct);
        }