BuildMatrix() public static method

public static BuildMatrix ( BitVector dataBits, ErrorCorrectionLevel ecLevel, int version, int maskPattern, ByteMatrix matrix ) : void
dataBits BitVector
ecLevel ErrorCorrectionLevel
version int
maskPattern int
matrix ByteMatrix
return void
コード例 #1
0
        private static int ChooseMaskPattern(BitVector bits, ErrorCorrectionLevel ecLevel, int version,
                                             ByteMatrix matrix)
        {
            var minPenalty      = int.MaxValue; // Lower penalty is better.
            var bestMaskPattern = -1;

            // We try all mask patterns to choose the best one.
            for (var maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++)
            {
                MatrixUtil.BuildMatrix(bits, ecLevel, version, maskPattern, matrix);
                var penalty = CalculateMaskPenalty(matrix);
                if (penalty < minPenalty)
                {
                    minPenalty      = penalty;
                    bestMaskPattern = maskPattern;
                }
            }
            return(bestMaskPattern);
        }
コード例 #2
0
        public static void Encode(string content, ErrorCorrectionLevel ecLevel, IDictionary <EncodeHintType, object> hints,
                                  QRCode qrCode)
        {
            string encoding = null;

            if (hints != null && hints.ContainsKey(EncodeHintType.CHARACTER_SET))
            {
                encoding = (string)hints[EncodeHintType.CHARACTER_SET];
            }

            if (encoding == null)
            {
                encoding = DEFAULT_BYTE_MODE_ENCODING;
            }

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

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

            AppendBytes(content, mode, dataBits, encoding);
            // Step 3: Initialize QR code that can contain "dataBits".
            var numInputBytes = dataBits.SizeInBytes();

            InitQRCode(numInputBytes, ecLevel, mode, qrCode);

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

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

            AppendModeInfo(mode, headerAndDataBits);

            var numLetters = mode.Equals(Mode.BYTE) ? dataBits.SizeInBytes() : content.Length;

            AppendLengthInfo(numLetters, qrCode.GetVersion(), mode, headerAndDataBits);
            headerAndDataBits.AppendBitVector(dataBits);

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

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

            InterleaveWithECBytes(headerAndDataBits, qrCode.GetNumTotalBytes(), qrCode.GetNumDataBytes(),
                                  qrCode.GetNumRSBlocks(), finalBits);

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

            qrCode.SetMaskPattern(ChooseMaskPattern(finalBits, qrCode.GetECLevel(), qrCode.GetVersion(),
                                                    matrix));

            // Step 8.  Build the matrix and set it to "qrCode".
            MatrixUtil.BuildMatrix(finalBits, qrCode.GetECLevel(), qrCode.GetVersion(),
                                   qrCode.GetMaskPattern(), matrix);
            qrCode.SetMatrix(matrix);
            // Step 9.  Make sure we have a valid QR Code.
            if (!qrCode.IsValid())
            {
                throw new WriterException("Invalid QR code: " + qrCode.ToString());
            }
        }