Exemple #1
0
        private byte[] CreateData(int typeNumber, ErrorCorrectLevel errorCorrectLevel, List <QR8BitByte> dataList)
        {
            var rsBlocks = QRRSBlock.GetRSBlocks(typeNumber, errorCorrectLevel);
            var buffer   = new QRBitBuffer();

            for (var i = 0; i < dataList.Count; i += 1)
            {
                var data = dataList[i];
                buffer.Put((int)data.Mode, 4);
                buffer.Put(data.Length, QRUtil.GetLengthInBits(data.Mode, typeNumber));
                data.Write(buffer);
            }

            // calc num max data.
            var totalDataCount = 0;

            for (var i = 0; i < rsBlocks.Length; i += 1)
            {
                totalDataCount += rsBlocks[i].DataCount;
            }

            if (buffer.LengthInBits > totalDataCount * 8)
            {
                throw new OverflowException(
                          $"code length overflow. ({buffer.LengthInBits}>{totalDataCount * 8})"
                          );
            }

            // end code
            if (buffer.LengthInBits + 4 <= totalDataCount * 8)
            {
                buffer.Put(0, 4);
            }

            // padding
            while (buffer.LengthInBits % 8 != 0)
            {
                buffer.PutBit(false);
            }

            // padding
            while (true)
            {
                if (buffer.LengthInBits >= totalDataCount * 8)
                {
                    break;
                }

                buffer.Put(Pad0, 8);

                if (buffer.LengthInBits >= totalDataCount * 8)
                {
                    break;
                }

                buffer.Put(Pad1, 8);
            }

            return(CreateBytes(buffer, rsBlocks));
        }
Exemple #2
0
 private static bool CheckErrorCorrectLevel(string errcor, out ErrorCorrectLevel errorCorrectLevel)
 {
     if (string.IsNullOrWhiteSpace(errcor))
     {
         errorCorrectLevel = ErrorCorrectLevel.M;
         return(true);
     }
     if (Enum.TryParse <ErrorCorrectLevel>(errcor, out errorCorrectLevel))
     {
         return(true);
     }
     LogError("Invalid error correct level.");
     return(false);
 }
Exemple #3
0
        public static QRRSBlock[] GetRSBlocks(int typeNumber, ErrorCorrectLevel errorCorrectLevel)
        {
            int[] rsBlock = GetRsBlockTable(typeNumber, errorCorrectLevel);

            int length = rsBlock.Length / 3;

            var list = new List <QRRSBlock>();

            for (int i = 0; i < length; i++)
            {
                int count      = rsBlock[i * 3 + 0];
                int totalCount = rsBlock[i * 3 + 1];
                int dataCount  = rsBlock[i * 3 + 2];
                for (int j = 0; j < count; j++)
                {
                    list.Add(new QRRSBlock(totalCount, dataCount));
                }
            }
            return(list.ToArray());
        }
Exemple #4
0
        private static int[] GetRsBlockTable(int typeNumber, ErrorCorrectLevel errorCorrectLevel)
        {
            switch (errorCorrectLevel)
            {
            case ErrorCorrectLevel.L:
                return(RsBlockTable[(typeNumber - 1) * 4 + 0]);

            case ErrorCorrectLevel.M:
                return(RsBlockTable[(typeNumber - 1) * 4 + 1]);

            case ErrorCorrectLevel.Q:
                return(RsBlockTable[(typeNumber - 1) * 4 + 2]);

            case ErrorCorrectLevel.H:
                return(RsBlockTable[(typeNumber - 1) * 4 + 3]);

            default:
                throw new ArgumentException("bad rs block @ typeNumber:" + typeNumber +
                                            "/errorCorrectLevel:" + errorCorrectLevel);
            }
        }
Exemple #5
0
 public QRCodeInternal(int typeNumber, ErrorCorrectLevel errorCorrectLevel)
 {
     _typeNumber        = typeNumber;
     _errorCorrectLevel = errorCorrectLevel;
     _dataList          = new List <QR8BitByte>();
 }