iTextSharp.text.pdf.qrcode.Encoder.Encode C# (CSharp) Method

Encode() public static method

public static Encode ( String content, ErrorCorrectionLevel ecLevel, Object>.IDictionary hints, QRCode qrCode ) : void
content String
ecLevel ErrorCorrectionLevel
hints Object>.IDictionary
qrCode QRCode
return void
        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).
            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.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.
            BitVector finalBits = new BitVector();
            InterleaveWithECBytes(headerAndDataBits, qrCode.GetNumTotalBytes(), qrCode.GetNumDataBytes(),
                qrCode.GetNumRSBlocks(), finalBits);

            // Step 7: Choose the mask pattern and set to "qrCode".
            ByteMatrix 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());
            }
        }

Same methods

Encoder::Encode ( String content, ErrorCorrectionLevel ecLevel, QRCode qrCode ) : void

Usage Example

        public ByteMatrix Encode(string contents, int width, int height,
                                 IDictionary <EncodeHintType, object> hints)
        {
            if (contents == null || contents.Length == 0)
            {
                throw new ArgumentException("Found empty contents");
            }

            if (width < 0 || height < 0)
            {
                throw new ArgumentException("Requested dimensions are too small: " + width + 'x' +
                                            height);
            }

            var errorCorrectionLevel = ErrorCorrectionLevel.L;

            if (hints != null && hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
            {
                errorCorrectionLevel = (ErrorCorrectionLevel)hints[EncodeHintType.ERROR_CORRECTION];
            }

            var code = new QRCode();

            Encoder.Encode(contents, errorCorrectionLevel, hints, code);
            return(RenderResult(code, width, height));
        }