示例#1
0
        public void InterleaveWithECBytes()
        {
            byte[]  dataBytes = { 32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236 };
            BitList inList    = new BitList();

            foreach (byte dataByte in dataBytes)
            {
                inList.AppendBits(dataByte, 8);
            }

            BitList outList = QREncoderMatrix.MixWithCorrectionBytes(inList, 26, 9, 1);

            byte[] expected =
            {
                // Data bytes.
                32,               65, (byte)205,        69,        41, (byte)220,        46, (byte)128, (byte)236,
                // Error correction bytes.
                42,        (byte)159,        74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138,70,
                (byte)237,        85, (byte)224,        96,        74, (byte)219,        61,
            };
            Assert.AreEqual(expected.Length, outList.ByteSize);
            byte[] outArray = new byte[expected.Length];
            outList.GetBytes(outArray, 0, 0, expected.Length);
            // Can't use Arrays.equals(), because outArray may be longer than out.sizeInBytes()
            for (int x = 0; x < expected.Length; x++)
            {
                Assert.AreEqual(expected[x], outArray[x]);
            }
            // Numbers are from http://www.swetake.com/qr/qr8.html
            dataBytes = new byte[] {
                67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182,
                (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135,
                (byte)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166,
                (byte)182, (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119,
                (byte)135, (byte)151, (byte)160, (byte)236, 17, (byte)236, 17, (byte)236, 17, (byte)236,
                17
            };
            inList = new BitList();
            foreach (byte dataByte in dataBytes)
            {
                inList.AppendBits(dataByte, 8);
            }

            outList  = QREncoderMatrix.MixWithCorrectionBytes(inList, 134, 62, 4);
            expected = new byte[] {
                // Data bytes.
                67, (byte)230, 54, 55, 70, (byte)247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39,
                118, 119, 70, 55, (byte)134, (byte)135, 86, 71, (byte)150, (byte)151, 102, 87, (byte)166,
                (byte)160, 118, 103, (byte)182, (byte)236, (byte)134, 119, (byte)198, 17, (byte)150,
                (byte)135, (byte)214, (byte)236, (byte)166, (byte)151, (byte)230, 17, (byte)182,
                (byte)166, (byte)247, (byte)236, (byte)198, 22, 7, 17, (byte)214, 38, 23, (byte)236, 39,
                17,
                // Error correction bytes.
                (byte)175, (byte)155, (byte)245, (byte)236, 80, (byte)146, 56, 74, (byte)155, (byte)165,
                (byte)133, (byte)142, 64, (byte)183, (byte)132, 13, (byte)178, 54, (byte)132, 108, 45,
                113, 53, 50, (byte)214, 98, (byte)193, (byte)152, (byte)233, (byte)147, 50, 71, 65,
                (byte)190, 82, 51, (byte)209, (byte)199, (byte)171, 54, 12, 112, 57, 113, (byte)155, 117,
                (byte)211, (byte)164, 117, 30, (byte)158, (byte)225, 31, (byte)190, (byte)242, 38,
                (byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
                (byte)187, 49, (byte)156, (byte)214,
            };
            Assert.AreEqual(expected.Length, outList.ByteSize);
            outArray = new byte[expected.Length];
            outList.GetBytes(outArray, 0, 0, expected.Length);
            for (int x = 0; x < expected.Length; x++)
            {
                Assert.AreEqual(expected[x], outArray[x]);
            }
        }
示例#2
0
        /// <summary>
        /// According to JISX0510:2004 8.6 p.37 bits are mixed mixes with their correction bytes.
        /// </summary>
        /// <param name="bits">Data bits</param>
        /// <param name="numTotalBytes">Total bytes count</param>
        /// <param name="numDataBytes">Data bytes count</param>
        /// <param name="rsBlocksQty">Reed/Solomon blocks count</param>
        /// <returns>Mixed bits</returns>
        public static BitList MixWithCorrectionBytes(BitList bits, int numTotalBytes, int numDataBytes, int rsBlocksQty)
        {
            if (bits.ByteSize != numDataBytes)
            {
                throw new AzosException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".interleaveWithECBytes(bits.ByteSize != numDataBytes)");
            }

            // Split data bytes into blocks. Generate error correction bytes.
            int dataBytesOffset = 0;
            int maxNumDataBytes = 0;
            int maxNumEcBytes   = 0;

            var blocks = new List <QRDataNCorrection>(rsBlocksQty);

            for (int i = 0; i < rsBlocksQty; ++i)
            {
                int[] numDataBytesInBlock = new int[1];
                int[] numEcBytesInBlock   = new int[1];
                GetNumDataBytesAndNumCorrectionBytesByBlockID(numTotalBytes, numDataBytes, rsBlocksQty, i, numDataBytesInBlock, numEcBytesInBlock);

                int    size      = numDataBytesInBlock[0];
                byte[] dataBytes = new byte[size];
                bits.GetBytes(dataBytes, 8 * dataBytesOffset, 0, size);
                byte[] ecBytes = GetCorrectionBytes(dataBytes, numEcBytesInBlock[0]);
                blocks.Add(new QRDataNCorrection(dataBytes, ecBytes));

                maxNumDataBytes  = Math.Max(maxNumDataBytes, size);
                maxNumEcBytes    = Math.Max(maxNumEcBytes, ecBytes.Length);
                dataBytesOffset += numDataBytesInBlock[0];
            }

            if (numDataBytes != dataBytesOffset)
            {
                throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".interleaveWithECBytes: numDataBytes!=dataBytesOffset");
            }

            BitList result = new BitList();

            for (int i = 0; i < maxNumDataBytes; ++i)
            {
                foreach (QRDataNCorrection block in blocks)
                {
                    byte[] dataBytes = block.Data;
                    if (i < dataBytes.Length)
                    {
                        result.AppendBits(dataBytes[i], 8);
                    }
                }
            }

            for (int i = 0; i < maxNumEcBytes; ++i)
            {
                foreach (QRDataNCorrection block in blocks)
                {
                    byte[] ecBytes = block.Correction;
                    if (i < ecBytes.Length)
                    {
                        result.AppendBits(ecBytes[i], 8);
                    }
                }
            }

            if (numTotalBytes != result.ByteSize)
            {
                throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".interleaveWithECBytes: numTotalBytes!=result.ByteSize");
            }

            return(result);
        }
示例#3
0
      /// <summary>
      /// According to JISX0510:2004 8.6 p.37 bits are mixed mixes with their correction bytes.
      /// </summary>
      /// <param name="bits">Data bits</param>
      /// <param name="numTotalBytes">Total bytes count</param>
      /// <param name="numDataBytes">Data bytes count</param>
      /// <param name="rsBlocksQty">Reed/Solomon blocks count</param>
      /// <returns>Mixed bits</returns>
      public static BitList MixWithCorrectionBytes(BitList bits, int numTotalBytes, int numDataBytes, int rsBlocksQty)
      {
        if (bits.ByteSize != numDataBytes)
          throw new NFXException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".interleaveWithECBytes(bits.ByteSize != numDataBytes)");

        // Split data bytes into blocks. Generate error correction bytes.
        int dataBytesOffset = 0;
        int maxNumDataBytes = 0;
        int maxNumEcBytes = 0;

        var blocks = new List<QRDataNCorrection>(rsBlocksQty);

        for (int i = 0; i < rsBlocksQty; ++i)
        {
          int[] numDataBytesInBlock = new int[1];
          int[] numEcBytesInBlock = new int[1];
          GetNumDataBytesAndNumCorrectionBytesByBlockID( numTotalBytes, numDataBytes, rsBlocksQty, i, numDataBytesInBlock, numEcBytesInBlock);

          int size = numDataBytesInBlock[0];
          byte[] dataBytes = new byte[size];
          bits.GetBytes(dataBytes, 8 * dataBytesOffset, 0, size);
          byte[] ecBytes = GetCorrectionBytes(dataBytes, numEcBytesInBlock[0]);
          blocks.Add(new QRDataNCorrection(dataBytes, ecBytes));

          maxNumDataBytes = Math.Max(maxNumDataBytes, size);
          maxNumEcBytes = Math.Max(maxNumEcBytes, ecBytes.Length);
          dataBytesOffset += numDataBytesInBlock[0];
        }

        if (numDataBytes != dataBytesOffset)
          throw new NFXException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".interleaveWithECBytes: numDataBytes!=dataBytesOffset");

        BitList result = new BitList();

        for (int i = 0; i < maxNumDataBytes; ++i)
          foreach (QRDataNCorrection block in blocks)
          {
            byte[] dataBytes = block.Data;
            if (i < dataBytes.Length)
              result.AppendBits(dataBytes[i], 8);
          }

        for (int i = 0; i < maxNumEcBytes; ++i)
          foreach (QRDataNCorrection block in blocks)
          {
            byte[] ecBytes = block.Correction;
            if (i < ecBytes.Length)
              result.AppendBits(ecBytes[i], 8);
          }

        if (numTotalBytes != result.ByteSize)
          throw new NFXException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".interleaveWithECBytes: numTotalBytes!=result.ByteSize");

        return result;
      }