示例#1
0
        public static void AppendNumericBytes(String content, BitList bits)
        {
            int length = content.Length;

            int i = 0;

            while (i < length)
            {
                int num1 = content[i] - '0';
                if (i + 2 < length)
                {
                    // 3 num letters => 10 bits
                    int num2 = content[i + 1] - '0';
                    int num3 = content[i + 2] - '0';
                    bits.AppendBits(num1 * 100 + num2 * 10 + num3, 10);
                    i += 3;
                }
                else if (i + 1 < length)
                {
                    // 2 num letters => 7 bits
                    int num2 = content[i + 1] - '0';
                    bits.AppendBits(num1 * 10 + num2, 7);
                    i += 2;
                }
                else
                {
                    // 1 num letters => 4 bits
                    bits.AppendBits(num1, 4);
                    i++;
                }
            }
        }
示例#2
0
        public static void GenerateTypeInfoBits(BitList bits, QRCorrectionLevel correctionLevel, int maskPattern)
        {
            if (!IsValidMaskPattern(maskPattern))
            {
                throw new AzosException(StringConsts.ARGUMENT_ERROR + typeof(QRMatrix).Name
                                        + ".GenerateTypeInfoBits(!QRCode.IsValidMaskPattern(maskPattern))");
            }

            int typeInfo = (correctionLevel.MarkerBits << 3) | maskPattern;

            bits.AppendBits(typeInfo, 5);

            int bchCode = CalculateBCHCode(typeInfo, TYPE_INFO_POLY);

            bits.AppendBits(bchCode, 10);

            BitList maskBits = new BitList();

            maskBits.AppendBits(TYPE_INFO_MASK_PATTERN, 15);
            bits.Xor(maskBits);

            if (bits.Size != 15)
            {
                throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QRMatrix).Name + ".makeTypeInfoBits: bits.Size != 15");
            }
        }
示例#3
0
        public static void AppendAlphanumericBytes(String content, BitList bits)
        {
            int length = content.Length;

            int i = 0;

            while (i < length)
            {
                int code1 = getAlphanumericCode(content[i]);
                if (code1 == -1)
                {
                    throw new AzosException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".appendAlphanumericBytes(content[i] in ALPHANUMERIC_TABLE)");
                }
                if (i + 1 < length)
                {
                    int code2 = getAlphanumericCode(content[i + 1]);
                    if (code2 == -1)
                    {
                        throw new AzosException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".appendAlphanumericBytes(content[i] in ALPHANUMERIC_TABLE)");
                    }
                    // 2 alphanum letters => 11 bits
                    bits.AppendBits(code1 * 45 + code2, 11);
                    i += 2;
                }
                else
                {
                    // 1 alphanum letters => 6 bits
                    bits.AppendBits(code1, 6);
                    i++;
                }
            }
        }
示例#4
0
        public static void GenerateVersionInfoBits(BitList bits, QRVersion version)
        {
            bits.AppendBits(version.Number, 6);
            int bchCode = CalculateBCHCode(version.Number, VERSION_INFO_POLY);

            bits.AppendBits(bchCode, 12);

            if (bits.Size != 18)
            {
                throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QRMatrix).Name + ".makeVersionInfoBits: bits.Size != 18");
            }
        }
示例#5
0
        public void TerminateBits()
        {
            BitList v = new BitList();

            QREncoderMatrix.WriteTerminationSection(0, v);
            Assert.AreEqual("", v.ToString());
            v = new BitList();
            QREncoderMatrix.WriteTerminationSection(1, v);
            Assert.AreEqual("00000000", v.ToString());
            v = new BitList();
            v.AppendBits(0, 3); // Append 000
            QREncoderMatrix.WriteTerminationSection(1, v);
            Assert.AreEqual("00000000", v.ToString());
            v = new BitList();
            v.AppendBits(0, 5); // Append 00000
            QREncoderMatrix.WriteTerminationSection(1, v);
            Assert.AreEqual("00000000", v.ToString());
            v = new BitList();
            v.AppendBits(0, 8); // Append 00000000
            QREncoderMatrix.WriteTerminationSection(1, v);
            Assert.AreEqual("00000000", v.ToString());
            v = new BitList();
            QREncoderMatrix.WriteTerminationSection(2, v);
            Assert.AreEqual("00000000 11101100", v.ToString());
            v = new BitList();
            v.AppendBits(0, 1); // Append 0
            QREncoderMatrix.WriteTerminationSection(3, v);
            Assert.AreEqual("00000000 11101100 00010001", v.ToString());
        }
示例#6
0
        public static void AppendLengthInfo(int numLetters, QRVersion version, QRMode mode, BitList bits)
        {
            int numBits = mode.GetVersionCharacterCount(version);

            if (numLetters >= (1 << numBits))
            {
                throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".appendLengthInfo(numLetters >= (1 << numBits))");
            }

            bits.AppendBits(numLetters, numBits);
        }
示例#7
0
        public void BuildMatrix()
        {
            const int WIDTH = 21, HEIGHT = 21;

            // From http://www.swetake.com/qr/qr7.html
            int[] ints =
            {
                32,  65, 205,  69,  41, 220,  46, 128, 236,
                42, 159,  74, 221, 244, 169, 239, 150, 138,
                70, 237,  85, 224,  96,  74, 219, 61
            };

            BitList bits = new BitList();

            foreach (int i in ints)
            {
                bits.AppendBits(i, 8);
            }

            QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);

            matrix.FormMatrix(bits, QRCorrectionLevel.H, QRVersion.GetVersionByNumber(1), 3);

            string expected =
                "1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\r\n" +
                "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" +
                "1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\r\n" +
                "1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\r\n" +
                "1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\r\n" +
                "1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\r\n" +
                "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" +
                "0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\r\n" +
                "0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\r\n" +
                "1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\r\n" +
                "1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\r\n" +
                "1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\r\n" +
                "0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\r\n" +
                "0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\r\n" +
                "1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\r\n" +
                "1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\r\n" +
                "1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\r\n" +
                "1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\r\n" +
                "1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\r\n" +
                "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\r\n" +
                "1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0";

            Assert.AreEqual(expected, matrix.ToString());
        }
示例#8
0
        public static QREncoderMatrix Encode(string content, QRCorrectionLevel correctionLevel)
        {
            string encoding = DEFAULT_ENCODING;

            QRMode mode = chooseMode(content, encoding);

            BitList header = new BitList();

            header.AppendBits(mode.ModeSignature, 4);

            BitList data = new BitList();

            AppendBytes(content, mode, data);

            int       provisionalBitsNeeded = header.Size + mode.GetVersionCharacterCount(QRVersion.GetVersionByNumber(1)) + data.Size;
            QRVersion provisionalVersion    = chooseVersion(provisionalBitsNeeded, correctionLevel);

            int       bitsNeeded = header.Size + mode.GetVersionCharacterCount(provisionalVersion) + data.Size;
            QRVersion version    = chooseVersion(bitsNeeded, correctionLevel);

            BitList headerNData = new BitList();

            headerNData.AppendBitList(header);

            int numLetters = mode == QRMode.BYTE ? data.ByteSize : content.Length;

            AppendLengthInfo(numLetters, version, mode, headerNData);

            headerNData.AppendBitList(data);

            QRVersion.CorrectionBlockSet correctionBlockSet = version.GetBlockSetByLevel(correctionLevel);
            int dataBytesQty = version.TotalCodewords - correctionBlockSet.TotalCodewords;

            WriteTerminationSection(dataBytesQty, headerNData);

            BitList finalBits = MixWithCorrectionBytes(headerNData, version.TotalCodewords, dataBytesQty, correctionBlockSet.TotalQty);

            int             dimension = version.Dimension;
            QREncoderMatrix matrix    = new QREncoderMatrix(dimension, content, correctionLevel, mode, version);

            int maskPattern = chooseMaskPattern(finalBits, correctionLevel, version, matrix);

            matrix.MaskPattern = maskPattern;

            matrix.FormMatrix(finalBits, correctionLevel, version, maskPattern);

            return(matrix);
        }
示例#9
0
 public static void Append8BitBytes(String content, BitList bits)
 {
     byte[] bytes;
     try
     {
         bytes = Encoding.GetEncoding(DEFAULT_ENCODING).GetBytes(content);
     }
     catch (Exception)
     {
         throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".append8BitBytes: Encoding.GetEncoding(DEFAULT_BYTE_MODE_ENCODING).GetBytes(content)");
     }
     foreach (byte b in bytes)
     {
         bits.AppendBits(b, 8);
     }
 }
示例#10
0
      public static QREncoderMatrix Encode(string content, QRCorrectionLevel correctionLevel)
      {
        string encoding = DEFAULT_ENCODING;

        QRMode mode = chooseMode(content, encoding);

        BitList header = new BitList();
        header.AppendBits(mode.ModeSignature, 4);

        BitList data = new BitList();
        AppendBytes(content, mode, data);

        int provisionalBitsNeeded = header.Size + mode.GetVersionCharacterCount(QRVersion.GetVersionByNumber(1)) + data.Size;
        QRVersion provisionalVersion = chooseVersion(provisionalBitsNeeded, correctionLevel);

        int bitsNeeded = header.Size + mode.GetVersionCharacterCount(provisionalVersion) + data.Size;
        QRVersion version = chooseVersion(bitsNeeded, correctionLevel);

        BitList headerNData = new BitList();

        headerNData.AppendBitList(header);

        int numLetters = mode == QRMode.BYTE ? data.ByteSize : content.Length;

        AppendLengthInfo(numLetters, version, mode, headerNData);

        headerNData.AppendBitList(data);

        QRVersion.CorrectionBlockSet correctionBlockSet = version.GetBlockSetByLevel(correctionLevel);
        int dataBytesQty = version.TotalCodewords - correctionBlockSet.TotalCodewords;

        WriteTerminationSection(dataBytesQty, headerNData);

        BitList finalBits = MixWithCorrectionBytes(headerNData, version.TotalCodewords, dataBytesQty, correctionBlockSet.TotalQty);

        int dimension = version.Dimension;
        QREncoderMatrix matrix = new QREncoderMatrix( dimension, content, correctionLevel, mode, version);
        
        int maskPattern = chooseMaskPattern(finalBits, correctionLevel, version, matrix);

        matrix.MaskPattern = maskPattern;

        matrix.FormMatrix(finalBits, correctionLevel, version, maskPattern);

        return matrix;
      }
示例#11
0
        public void BuildMatrix()
        {
            const int WIDTH = 21, HEIGHT = 21;

              // From http://www.swetake.com/qr/qr7.html
              int[] ints = {
              32, 65, 205, 69, 41, 220, 46, 128, 236,
              42, 159, 74, 221, 244, 169, 239, 150, 138,
              70, 237, 85, 224, 96, 74, 219 , 61};

              BitList bits = new BitList();
              foreach (int i in ints)
            bits.AppendBits(i, 8);

              QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);

              matrix.FormMatrix(bits, QRCorrectionLevel.H, QRVersion.GetVersionByNumber(1), 3);

              string expected =
            "1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\r\n" +
            "1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\r\n" +
            "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" +
            "0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\r\n" +
            "0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\r\n" +
            "1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\r\n" +
            "1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\r\n" +
            "1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\r\n" +
            "0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\r\n" +
            "0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\r\n" +
            "1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\r\n" +
            "1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\r\n" +
            "1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\r\n" +
            "1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\r\n" +
            "1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0";

              Assert.AreEqual( expected, matrix.ToString());
        }
示例#12
0
        /// <summary>
        /// Write termination section according 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
        /// </summary>
        public static void WriteTerminationSection(int dataBytesQty, BitList bits)
        {
            int capacity = dataBytesQty << 3;

            if (bits.Size > capacity)
            {
                throw new AzosException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".writeTerminationSection(bits.Size>(dataBytesQty<<3))");
            }

            for (int i = 0; i < 4 && bits.Size < capacity; ++i)
            {
                bits.AppendBit(false);
            }

            // Termination bits (JISX0510:2004 p.24 8.4.8)
            // Add padding bits when last byte isn't 8bit aligned
            int numBitsInLastByte = bits.Size & 0x07;

            if (numBitsInLastByte > 0)
            {
                for (int i = numBitsInLastByte; i < 8; i++)
                {
                    bits.AppendBit(false);
                }
            }

            // According to JISX0510:2004 8.4.9 p.24 fill possible free space with pre-defined pattern
            int numPaddingBytes = dataBytesQty - bits.ByteSize;

            for (int i = 0; i < numPaddingBytes; ++i)
            {
                bits.AppendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8);
            }

            if (bits.Size != capacity)
            {
                throw new AzosException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".writeTerminationSection: bits.Size!=capacity");
            }
        }
示例#13
0
      public static void AppendLengthInfo(int numLetters, QRVersion version, QRMode mode, BitList bits)
      {
         int numBits = mode.GetVersionCharacterCount(version);
         if (numLetters >= (1 << numBits))
           throw new NFXException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".appendLengthInfo(numLetters >= (1 << numBits))");

         bits.AppendBits(numLetters, numBits);
      }
示例#14
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;
      }
示例#15
0
      public static void AppendNumericBytes(String content, BitList bits)
      {
        int length = content.Length;

        int i = 0;
        while (i < length)
        {
          int num1 = content[i] - '0';
          if (i + 2 < length)
          {
              // 3 num letters => 10 bits
              int num2 = content[i + 1] - '0';
              int num3 = content[i + 2] - '0';
              bits.AppendBits(num1 * 100 + num2 * 10 + num3, 10);
              i += 3;
          }
          else if (i + 1 < length)
          {
              // 2 num letters => 7 bits
              int num2 = content[i + 1] - '0';
              bits.AppendBits(num1 * 10 + num2, 7);
              i += 2;
          }
          else
          {
              // 1 num letters => 4 bits
              bits.AppendBits(num1, 4);
              i++;
          }
        }
      }
示例#16
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);
        }
示例#17
0
      public static void AppendAlphanumericBytes(String content, BitList bits)
      {
        int length = content.Length;

        int i = 0;
        while (i < length)
        {
          int code1 = getAlphanumericCode(content[i]);
          if (code1 == -1)
          {
              throw new NFXException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".appendAlphanumericBytes(content[i] in ALPHANUMERIC_TABLE)");
          }
          if (i + 1 < length)
          {
              int code2 = getAlphanumericCode(content[i + 1]);
              if (code2 == -1)
              {
                throw new NFXException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".appendAlphanumericBytes(content[i] in ALPHANUMERIC_TABLE)");
              }
              // 2 alphanum letters => 11 bits
              bits.AppendBits(code1 * 45 + code2, 11);
              i += 2;
          }
          else
          {
            // 1 alphanum letters => 6 bits
            bits.AppendBits(code1, 6);
            i++;
          }
        }
      }
示例#18
0
 public static void Append8BitBytes(String content, BitList bits)
 {
   byte[] bytes;
   try
   {
     bytes = Encoding.GetEncoding(DEFAULT_ENCODING).GetBytes(content);
   }
   catch (Exception)
   {
     throw new NFXException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".append8BitBytes: Encoding.GetEncoding(DEFAULT_BYTE_MODE_ENCODING).GetBytes(content)");
   }
   foreach (byte b in bytes)
   {
     bits.AppendBits(b, 8);
   }
 }
示例#19
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]);
            }
        }
示例#20
0
      /// <summary>
      /// Write termination section according 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
      /// </summary>
      public static void WriteTerminationSection(int dataBytesQty, BitList bits)
      {
        int capacity = dataBytesQty << 3;
        if (bits.Size > capacity)
          throw new NFXException(StringConsts.ARGUMENT_ERROR + typeof(QREncoderMatrix).Name + ".writeTerminationSection(bits.Size>(dataBytesQty<<3))");

        for (int i = 0; i < 4 && bits.Size < capacity; ++i)
          bits.AppendBit(false);

        // Termination bits (JISX0510:2004 p.24 8.4.8)
        // Add padding bits when last byte isn't 8bit aligned
        int numBitsInLastByte = bits.Size & 0x07;
        if (numBitsInLastByte > 0)
          for (int i = numBitsInLastByte; i < 8; i++)
              bits.AppendBit(false);

        // According to JISX0510:2004 8.4.9 p.24 fill possible free space with pre-defined pattern
        int numPaddingBytes = dataBytesQty - bits.ByteSize;
        for (int i = 0; i < numPaddingBytes; ++i)
          bits.AppendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8);

        if (bits.Size != capacity)
          throw new NFXException(StringConsts.CODE_LOGIC_ERROR + typeof(QREncoderMatrix).Name + ".writeTerminationSection: bits.Size!=capacity");
      }
示例#21
0
 public void TerminateBits()
 {
     BitList v = new BitList();
       QREncoderMatrix.WriteTerminationSection(0, v);
       Assert.AreEqual("", v.ToString());
       v = new BitList();
       QREncoderMatrix.WriteTerminationSection(1, v);
       Assert.AreEqual("00000000", v.ToString());
       v = new BitList();
       v.AppendBits(0, 3);  // Append 000
       QREncoderMatrix.WriteTerminationSection(1, v);
       Assert.AreEqual("00000000", v.ToString());
       v = new BitList();
       v.AppendBits(0, 5);  // Append 00000
       QREncoderMatrix.WriteTerminationSection(1, v);
       Assert.AreEqual("00000000", v.ToString());
       v = new BitList();
       v.AppendBits(0, 8);  // Append 00000000
       QREncoderMatrix.WriteTerminationSection(1, v);
       Assert.AreEqual("00000000", v.ToString());
       v = new BitList();
       QREncoderMatrix.WriteTerminationSection(2, v);
       Assert.AreEqual("00000000 11101100", v.ToString());
       v = new BitList();
       v.AppendBits(0, 1);  // Append 0
       QREncoderMatrix.WriteTerminationSection(3, v);
       Assert.AreEqual("00000000 11101100 00010001", v.ToString());
 }
示例#22
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]);
              }
        }