Example #1
0
        /// <summary> Append length info. On success, store the result in "bits".</summary>
        internal static void  appendLengthInfo(int numLetters, int version, Mode mode, BitVector bits)
        {
            int numBits = mode.getCharacterCountBits(Version.getVersionForNumber(version));

            if (numLetters > ((1 << numBits) - 1))
            {
                throw new WriterException(numLetters + "is bigger than" + ((1 << numBits) - 1));
            }
            bits.appendBits(numLetters, numBits);
        }
Example #2
0
 public QRCode()
 {
     mode          = null;
     ecLevel       = null;
     version       = -1;
     matrixWidth   = -1;
     maskPattern   = -1;
     numTotalBytes = -1;
     numDataBytes  = -1;
     numECBytes    = -1;
     numRSBlocks   = -1;
     matrix        = null;
 }
Example #3
0
 /// <summary> Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".</summary>
 internal static void  appendBytes(System.String content, Mode mode, BitVector bits, System.String encoding)
 {
     if (mode.Equals(Mode.NUMERIC))
     {
         appendNumericBytes(content, bits);
     }
     else if (mode.Equals(Mode.ALPHANUMERIC))
     {
         AppendAlphanumericBytes(content, bits);
     }
     else if (mode.Equals(Mode.BYTE))
     {
         Append8BitBytes(content, bits, encoding);
     }
     else if (mode.Equals(Mode.KANJI))
     {
         AppendKanjiBytes(content, bits);
     }
     else
     {
         throw new WriterException("Invalid mode: " + mode);
     }
 }
Example #4
0
		/// <summary> Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".</summary>
		internal static void  appendBytes(System.String content, Mode mode, BitVector bits, System.String encoding)
		{
			if (mode.Equals(Mode.NUMERIC))
			{
				appendNumericBytes(content, bits);
			}
			else if (mode.Equals(Mode.ALPHANUMERIC))
			{
				AppendAlphanumericBytes(content, bits);
			}
			else if (mode.Equals(Mode.BYTE))
			{
				Append8BitBytes(content, bits, encoding);
			}
			else if (mode.Equals(Mode.KANJI))
			{
				AppendKanjiBytes(content, bits);
			}
			else
			{
				throw new WriterException("Invalid mode: " + mode);
			}
		}
Example #5
0
		/// <summary> Append length info. On success, store the result in "bits".</summary>
		internal static void  appendLengthInfo(int numLetters, int version, Mode mode, BitVector bits)
		{
			int numBits = mode.getCharacterCountBits(Version.getVersionForNumber(version));
			if (numLetters > ((1 << numBits) - 1))
			{
				throw new WriterException(numLetters + "is bigger than" + ((1 << numBits) - 1));
			}
			bits.appendBits(numLetters, numBits);
		}
Example #6
0
		/// <summary> Append mode info. On success, store the result in "bits".</summary>
		internal static void  appendModeInfo(Mode mode, BitVector bits)
		{
			bits.appendBits(mode.Bits, 4);
		}
Example #7
0
		/// <summary> Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
		/// modify "qrCode".
		/// </summary>
		private static void  InitQrCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode, QRCode qrCode)
		{
			qrCode.ECLevel = ecLevel;
			qrCode.Mode = mode;
			
			// In the following comments, we use numbers of Version 7-H.
			for (int versionNum = 1; versionNum <= 40; versionNum++)
			{
				Version version = Version.getVersionForNumber(versionNum);
				// numBytes = 196
				int numBytes = version.TotalCodewords;
				// getNumECBytes = 130
				Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
				int numEcBytes = ecBlocks.TotalECCodewords;
				// getNumRSBlocks = 5
				int numRSBlocks = ecBlocks.NumBlocks;
				// getNumDataBytes = 196 - 130 = 66
				int numDataBytes = numBytes - numEcBytes;
				// We want to choose the smallest version which can contain data of "numInputBytes" + some
				// extra bits for the header (mode info and length info). The header can be three bytes
				// (precisely 4 + 16 bits) at most. Hence we do +3 here.
				if (numDataBytes >= numInputBytes + 3)
				{
					// Yay, we found the proper rs block info!
					qrCode.Version = versionNum;
					qrCode.NumTotalBytes = numBytes;
					qrCode.NumDataBytes = numDataBytes;
					qrCode.NumRSBlocks = numRSBlocks;
					// getNumECBytes = 196 - 66 = 130
					qrCode.NumECBytes = numEcBytes;
					// matrix width = 21 + 6 * 4 = 45
					qrCode.MatrixWidth = version.DimensionForVersion;
					return ;
				}
			}
			throw new WriterException("Cannot find proper rs block info (input data too big?)");
		}
Example #8
0
        public static void  Encode(System.String content, ErrorCorrectionLevel ecLevel, System.Collections.Hashtable hints, QRCode qrCode)
        {
            System.String encoding = hints == null?null:(System.String)hints[EncodeHintType.CHARACTER_SET];
            if (encoding == null)
            {
                encoding = DEFAULT_BYTE_MODE_ENCODING;
            }

            // Step 1: Choose the mode (encoding).
            Mode mode = ChooseMode(content, encoding);

            // Step 2: Append "bytes" into "dataBits" in appropriate encoding.
            BitVector dataBits = new BitVector();

            appendBytes(content, mode, dataBits, encoding);
            // Step 3: Initialize QR code that can contain "dataBits".
            int numInputBytes = dataBits.sizeInBytes();

            InitQrCode(numInputBytes, ecLevel, mode, qrCode);

            // Step 4: Build another bit vector that contains header and data.
            BitVector headerAndDataBits = new BitVector();

            // Step 4.5: Append ECI message if applicable
            if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding))
            {
                CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
                if (eci != null)
                {
                    AppendECI(eci, headerAndDataBits);
                }
            }

            appendModeInfo(mode, headerAndDataBits);

            int numLetters = mode.Equals(Mode.BYTE)?dataBits.sizeInBytes():content.Length;

            appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits);
            headerAndDataBits.appendBitVector(dataBits);

            // Step 5: Terminate the bits properly.
            TerminateBits(qrCode.NumDataBytes, headerAndDataBits);

            // Step 6: Interleave data bits with error correction code.
            BitVector finalBits = new BitVector();

            InterleaveWithEcBytes(headerAndDataBits, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits);

            // Step 7: Choose the mask pattern and set to "qrCode".
            ByteMatrix matrix = new ByteMatrix(qrCode.MatrixWidth, qrCode.MatrixWidth);

            qrCode.MaskPattern = ChooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix);

            // Step 8.  Build the matrix and set it to "qrCode".
            MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix);
            qrCode.Matrix = matrix;
            // Step 9.  Make sure we have a valid QR Code.
            if (!qrCode.Valid)
            {
                throw new WriterException(string.Format("Invalid QR code: {0}", qrCode));
            }
        }
Example #9
0
 /// <summary> Append mode info. On success, store the result in "bits".</summary>
 internal static void  appendModeInfo(Mode mode, BitVector bits)
 {
     bits.appendBits(mode.Bits, 4);
 }
Example #10
0
        /// <summary> Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
        /// modify "qrCode".
        /// </summary>
        private static void  InitQrCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode, QRCode qrCode)
        {
            qrCode.ECLevel = ecLevel;
            qrCode.Mode    = mode;

            // In the following comments, we use numbers of Version 7-H.
            for (int versionNum = 1; versionNum <= 40; versionNum++)
            {
                Version version = Version.getVersionForNumber(versionNum);
                // numBytes = 196
                int numBytes = version.TotalCodewords;
                // getNumECBytes = 130
                Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
                int numEcBytes            = ecBlocks.TotalECCodewords;
                // getNumRSBlocks = 5
                int numRSBlocks = ecBlocks.NumBlocks;
                // getNumDataBytes = 196 - 130 = 66
                int numDataBytes = numBytes - numEcBytes;
                // We want to choose the smallest version which can contain data of "numInputBytes" + some
                // extra bits for the header (mode info and length info). The header can be three bytes
                // (precisely 4 + 16 bits) at most. Hence we do +3 here.
                if (numDataBytes >= numInputBytes + 3)
                {
                    // Yay, we found the proper rs block info!
                    qrCode.Version       = versionNum;
                    qrCode.NumTotalBytes = numBytes;
                    qrCode.NumDataBytes  = numDataBytes;
                    qrCode.NumRSBlocks   = numRSBlocks;
                    // getNumECBytes = 196 - 66 = 130
                    qrCode.NumECBytes = numEcBytes;
                    // matrix width = 21 + 6 * 4 = 45
                    qrCode.MatrixWidth = version.DimensionForVersion;
                    return;
                }
            }
            throw new WriterException("Cannot find proper rs block info (input data too big?)");
        }