Beispiel #1
0
        /// <summary>
        /// Use number of data bits(header + eci header + data bits from EncoderBase) to search for proper version to use
        /// between min and max boundary.
        /// Boundary define by DynamicSearchIndicator method.
        /// </summary>
        private static int BinarySearch(int numDataBits, ErrorCorrectionLevel level, int lowerVersionNum, int higherVersionNum)
        {
            int middleVersionNumber;

            while (lowerVersionNum <= higherVersionNum)
            {
                middleVersionNumber = (lowerVersionNum + higherVersionNum) / 2;
                QRCodeVersion version        = VersionTable.GetVersionByNum(middleVersionNumber);
                int           numECCodewords = version.GetECBlocksByLevel(level).NumErrorCorrectionCodewards;
                int           dataCodewords  = version.TotalCodewords - numECCodewords;

                if (dataCodewords << 3 == numDataBits)
                {
                    return(middleVersionNumber);
                }

                if (dataCodewords << 3 > numDataBits)
                {
                    higherVersionNum = middleVersionNumber - 1;
                }
                else
                {
                    lowerVersionNum = middleVersionNumber + 1;
                }
            }
            return(lowerVersionNum);
        }
		/// <summary>
		/// Decide which version group it belong to
		/// </summary>
		/// <param name="numBits">Number of bits for bitlist where it contain DataBits encode from input content and ECI header</param>
		/// <param name="level">Error correction level</param>
		/// <returns>Version group index for VERSION_GROUP</returns>
		private static int DynamicSearchIndicator(int numBits, ErrorCorrectionLevel level)
		{
			int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet();
			int loopLength = VERSION_GROUP.Length;
			for (int i = 0; i < loopLength; i++)
			{
				int totalBits = numBits + NumBitsModeIndicator + charCountIndicator[i];

				QRCodeVersion version = VersionTable.GetVersionByNum(VERSION_GROUP[i]);
				int numECCodewords = version.GetECBlocksByLevel(level).NumErrorCorrectionCodewards;

				int dataCodewords = version.TotalCodewords - numECCodewords;

				if (totalBits <= dataCodewords * 8)
				{
					return i;
				}
			}

			throw new InputOutOfBoundaryException($"QRCode do not have enough space for {(numBits + NumBitsModeIndicator + charCountIndicator[2])} bits");
		}
Beispiel #3
0
        /// <summary>
        /// Decide which version group it belong to
        /// </summary>
        /// <param name="numBits">number of bits for bitlist where it contain DataBits encode from input content and ECI header</param>
        /// <param name="level">Error correction level</param>
        /// <param name="mode">Mode</param>
        /// <returns>Version group index for VERSION_GROUP</returns>
        private static int DynamicSearchIndicator(int numBits, ErrorCorrectionLevel level, Mode mode)
        {
            int[] charCountIndicator = CharCountIndicatorTable.GetCharCountIndicatorSet(mode);
            int   totalBits          = 0;
            int   loopLength         = VERSION_GROUP.Length;

            for (int i = 0; i < loopLength; i++)
            {
                totalBits = numBits + NUM_BITS_MODE_INDICATOR + charCountIndicator[i];

                QRCodeVersion version        = VersionTable.GetVersionByNum(VERSION_GROUP[i]);
                int           numECCodewords = version.GetECBlocksByLevel(level).NumErrorCorrectionCodewards;

                int dataCodewords = version.TotalCodewords - numECCodewords;

                if (totalBits <= dataCodewords * 8)
                {
                    return(i);
                }
            }

            throw new InputOutOfBoundaryException(string.Format("QRCode do not have enough space for {0} bits", (numBits + NUM_BITS_MODE_INDICATOR + charCountIndicator[2])));
        }
Beispiel #4
0
        private static VersionControlStruct FillVCStruct(int versionNum, ErrorCorrectionLevel level)
        {
            if (versionNum < 1 || versionNum > 40)
            {
                throw new InvalidOperationException($"Unexpected version number: {versionNum}");
            }

            VersionControlStruct vcStruct = new VersionControlStruct();

            int version = versionNum;

            QRCodeVersion versionData = VersionTable.GetVersionByNum(versionNum);

            int numTotalBytes = versionData.TotalCodewords;

            ErrorCorrectionBlocks ecBlocks = versionData.GetECBlocksByLevel(level);
            int numDataBytes = numTotalBytes - ecBlocks.NumErrorCorrectionCodewards;
            int numECBlocks  = ecBlocks.NumBlocks;

            VersionDetail vcDetail = new VersionDetail(version, numTotalBytes, numDataBytes, numECBlocks);

            vcStruct.VersionDetail = vcDetail;
            return(vcStruct);
        }