コード例 #1
0
        private byte[] CreateData(int errorCorrectLevel)
        {
            RSBlock[] rsBlocks = RSBlock.GetRSBlocks(errorCorrectLevel);

            BitBuffer buffer = new BitBuffer();

            buffer.Put(4, 4);
            buffer.Put(qrData.Length, 8);
            for (int i = 0; i < qrData.Length; i++)
            {
                buffer.Put(qrData[i], 8);
            }

            int totalDataCount = 0;

            for (int i = 0; i < rsBlocks.Length; i++)
            {
                totalDataCount += rsBlocks[i].GetDataCount();
            }

            if (buffer.GetLengthInBits() > totalDataCount * 8)
            {
                throw new ArgumentException("String length overflow. ("
                                            + buffer.GetLengthInBits()
                                            + ">"
                                            + totalDataCount * 8
                                            + ")");
            }

            if (buffer.GetLengthInBits() + 4 <= totalDataCount * 8)
            {
                buffer.Put(0, 4);
            }

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

            // padding
            while (true)
            {
                if (buffer.GetLengthInBits() >= totalDataCount * 8)
                {
                    break;
                }
                buffer.Put(PAD0, 8);

                if (buffer.GetLengthInBits() >= totalDataCount * 8)
                {
                    break;
                }
                buffer.Put(PAD1, 8);
            }

            return(CreateBytes(buffer, rsBlocks));
        }
コード例 #2
0
ファイル: QRCode.cs プロジェクト: Jackjet/ECOSingle
        private byte[] CreateData(int errorCorrectLevel)
        {
            RSBlock[] rSBlocks  = RSBlock.GetRSBlocks(errorCorrectLevel);
            BitBuffer bitBuffer = new BitBuffer();

            bitBuffer.Put(4, 4);
            bitBuffer.Put(this.qrData.Length, 8);
            for (int i = 0; i < this.qrData.Length; i++)
            {
                bitBuffer.Put((int)this.qrData[i], 8);
            }
            int num = 0;

            for (int j = 0; j < rSBlocks.Length; j++)
            {
                num += rSBlocks[j].GetDataCount();
            }
            if (bitBuffer.GetLengthInBits() > num * 8)
            {
                throw new ArgumentException(string.Concat(new object[]
                {
                    "String length overflow. (",
                    bitBuffer.GetLengthInBits(),
                    ">",
                    num * 8,
                    ")"
                }));
            }
            if (bitBuffer.GetLengthInBits() + 4 <= num * 8)
            {
                bitBuffer.Put(0, 4);
            }
            while (bitBuffer.GetLengthInBits() % 8 != 0)
            {
                bitBuffer.Put(false);
            }
            while (bitBuffer.GetLengthInBits() < num * 8)
            {
                bitBuffer.Put(236, 8);
                if (bitBuffer.GetLengthInBits() >= num * 8)
                {
                    break;
                }
                bitBuffer.Put(17, 8);
            }
            return(QRCode.CreateBytes(bitBuffer, rSBlocks));
        }
コード例 #3
0
        public static RSBlock[] GetRSBlocks(int errorCorrectLevel)
        {
            int[]          rsBlockTable = RSBlock.GetRsBlockTable(errorCorrectLevel);
            int            num          = rsBlockTable.Length / 3;
            List <RSBlock> list         = new List <RSBlock>();

            for (int i = 0; i < num; i++)
            {
                int num2 = rsBlockTable[3 * i];
                int num3 = rsBlockTable[3 * i + 1];
                int num4 = rsBlockTable[3 * i + 2];
                for (int j = 0; j < num2; j++)
                {
                    list.Add(new RSBlock(num3, num4));
                }
            }
            return(list.ToArray());
        }
コード例 #4
0
ファイル: QRCode.cs プロジェクト: lubota/spartacus
        private static byte[] CreateBytes(BitBuffer buffer, RSBlock[] rsBlocks)
        {
            int offset = 0;
            int maxDcCount = 0;
            int maxEcCount = 0;

            int[][] dcdata = new int[rsBlocks.Length][];
            int[][] ecdata = new int[rsBlocks.Length][];

            for (int r = 0; r < rsBlocks.Length; r++) {
            int dcCount = rsBlocks[r].GetDataCount();
            int ecCount = rsBlocks[r].GetTotalCount() - dcCount;

            maxDcCount = Math.Max(maxDcCount, dcCount);
            maxEcCount = Math.Max(maxEcCount, ecCount);

            dcdata[r] = new int[dcCount];
            for (int i = 0; i < dcdata[r].Length; i++) {
                dcdata[r][i] = 0xff & buffer.GetBuffer()[i + offset];
            }
            offset += dcCount;

            Polynomial rsPoly = QRUtil.GetErrorCorrectPolynomial(ecCount);
            Polynomial rawPoly = new Polynomial(dcdata[r], rsPoly.GetLength() - 1);

            Polynomial modPoly = rawPoly.Mod(rsPoly);
            ecdata[r] = new int[rsPoly.GetLength() - 1];
            for (int i = 0; i < ecdata[r].Length; i++) {
                int modIndex = i + modPoly.GetLength() - ecdata[r].Length;
                ecdata[r][i] = (modIndex >= 0)? modPoly.Get(modIndex) : 0;
            }
            }

            int totalCodeCount = 0;
            for (int i = 0; i < rsBlocks.Length; i++) {
            totalCodeCount += rsBlocks[i].GetTotalCount();
            }

            byte[] data = new byte[totalCodeCount];
            int index = 0;
            for (int i = 0; i < maxDcCount; i++) {
            for (int r = 0; r < rsBlocks.Length; r++) {
                if (i < dcdata[r].Length) {
                    data[index++] = (byte) dcdata[r][i];
                }
            }
            }

            for (int i = 0; i < maxEcCount; i++) {
            for (int r = 0; r < rsBlocks.Length; r++) {
                if (i < ecdata[r].Length) {
                    data[index++] = (byte) ecdata[r][i];
                }
            }
            }

            return data;
        }