예제 #1
0
        /// <summary>
        /// Calculate <c>QR</c> code format
        /// </summary>
        /// <param name="error">
        /// The error code information.
        /// </param>
        /// <param name="mask">
        /// The mask.
        /// </param>
        private void CalculateFormat(QrError error, QrMask mask)
        {
            // Get error bits
            if (this.debugData != null)
            {
                this.debugData.Append("- Calculating format bits...\n");
            }

            // Get 5 bits of format information
            int formatInfo = (QrErrorBits.GetBits(error) << 3) | BinaryExtensions.BinToDec(mask.GetBits());

            if (this.debugData != null)
            {
                this.debugData.Append(
                    "- Format information error and mask bits: "
                    + BinaryExtensions.BinaryToString(BinaryExtensions.DecToBin(formatInfo)) + "\n");
                this.debugData.Append("- Calculating BCH (15,5) code for information bits...\n");
            }

            int bchCode = QrBoseChaudhuriHocquenghem.CalculateBch(formatInfo, QrConst.GenPolyFormat);

            if (this.debugData != null)
            {
                this.debugData.Append(
                    "- BCH (15,5) error code: " + BinaryExtensions.BinaryToString(BinaryExtensions.DecToBin(bchCode))
                    + "\n");
            }

            // Append BCH code to format info
            formatInfo = (formatInfo << 10) | bchCode;

            if (this.debugData != null)
            {
                this.debugData.Append(
                    "- Format information with appended BCH: "
                    + BinaryExtensions.BinaryToString(BinaryExtensions.DecToBin(formatInfo)) + "\n");
                this.debugData.Append(
                    "- Calculating XOR with format constant: "
                    + BinaryExtensions.BinaryToString(BinaryExtensions.DecToBin(QrConst.ConstFormatXor)) + "\n");
            }

            formatInfo = formatInfo ^ QrConst.ConstFormatXor;

            if (this.debugData != null)
            {
                this.debugData.Append(
                    "- Final format: " + BinaryExtensions.BinaryToString(BinaryExtensions.DecToBin(formatInfo, 15))
                    + "\n");
            }

            List <bool> formatBits = BinaryExtensions.DecToBin(formatInfo, 15);

            // Reverse, because our QRCode is inserting backwards
            formatBits.Reverse();

            foreach (bool t in formatBits)
            {
                this.output.Enqueue(t);
            }
        }
예제 #2
0
        /// <summary>
        /// The write version.
        /// </summary>
        /// <param name="codeVersion">
        /// The version.
        /// </param>
        private void WriteVersion(QrVersion codeVersion)
        {
            if (this.debugData != null)
            {
                this.debugData.Append("- Should we write version information bits?\n");
            }

            // Only writing version with versions higher than 6
            if (codeVersion.Version >= 7)
            {
                // Calculate BCH (18, 6) for version information
                int bchVersion = QrBoseChaudhuriHocquenghem.CalculateBch(codeVersion.Version, QrConst.GenPolyVersion);

                int versionInformation = (codeVersion.Version << 12) | bchVersion;

                List <bool> versionInfo = BinaryExtensions.DecToBin(versionInformation, 18);

                versionInfo.Reverse();

                if (this.debugData != null)
                {
                    this.debugData.Append(
                        "- Yes, writing information: " + BinaryExtensions.BinaryToString(versionInfo) + ".\n");
                }

                int iterator = 0;

                // Version bits at upper right corner
                for (int y = 0; y < 6; y++)
                {
                    for (int x = this.picture.GetLength(0) - 8 - 3, j = 0; j < 3; x++, j++)
                    {
                        this.picture[x, y] = versionInfo[iterator] ? QrPixel.Black : QrPixel.White;

                        iterator++;
                    }
                }

                iterator = 0;

                // Version bits at lower left corner
                for (int x = 0; x < 6; x++)
                {
                    for (int y = this.picture.GetLength(0) - 8 - 3, j = 0; j < 3; y++, j++)
                    {
                        this.picture[x, y] = versionInfo[iterator] ? QrPixel.Black : QrPixel.White;

                        iterator++;
                    }
                }

                if (this.debugData != null)
                {
                    this.debugData.Append("- Version information bits successfully written.\n");
                }
            }

            if ((this.debugData != null) && (codeVersion.Version < 7))
            {
                this.debugData.Append("- No, only required for versions higher than 6.\n");
            }
        }