예제 #1
0
 /// <summary>Create a QR-code object with unitialized parameters</summary>
 public QRCode()
 {
     mode          = null;
     ecLevel       = null;
     version       = -1;
     matrixWidth   = -1;
     maskPattern   = -1;
     numTotalBytes = -1;
     numDataBytes  = -1;
     numECBytes    = -1;
     numRSBlocks   = -1;
     matrix        = null;
 }
예제 #2
0
        /// <exception cref="iText.Barcodes.Qrcode.WriterException"/>
        private static int ChooseMaskPattern(BitVector bits, ErrorCorrectionLevel ecLevel, int version, ByteMatrix
                                             matrix)
        {
            int minPenalty = int.MaxValue;
            // Lower penalty is better.
            int bestMaskPattern = -1;

            // We try all mask patterns to choose the best one.
            for (int maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++)
            {
                MatrixUtil.BuildMatrix(bits, ecLevel, version, maskPattern, matrix);
                int penalty = CalculateMaskPenalty(matrix);
                if (penalty < minPenalty)
                {
                    minPenalty      = penalty;
                    bestMaskPattern = maskPattern;
                }
            }
            return(bestMaskPattern);
        }
예제 #3
0
 /// <summary>Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode".</summary>
 /// <remarks>
 /// Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
 /// modify "qrCode".
 /// </remarks>
 /// <exception cref="iText.Barcodes.Qrcode.WriterException"/>
 private static void InitQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, int desiredMinVersion, Mode
                                mode, QRCode qrCode)
 {
     qrCode.SetECLevel(ecLevel);
     qrCode.SetMode(mode);
     // In the following comments, we use numbers of Version 7-H.
     for (int versionNum = desiredMinVersion; versionNum <= 40; versionNum++)
     {
         Version version = Version.GetVersionForNumber(versionNum);
         // numBytes = 196
         int numBytes = version.GetTotalCodewords();
         // getNumECBytes = 130
         Version.ECBlocks ecBlocks = version.GetECBlocksForLevel(ecLevel);
         int numEcBytes            = ecBlocks.GetTotalECCodewords();
         // getNumRSBlocks = 5
         int numRSBlocks = ecBlocks.GetNumBlocks();
         // 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.SetVersion(versionNum);
             qrCode.SetNumTotalBytes(numBytes);
             qrCode.SetNumDataBytes(numDataBytes);
             qrCode.SetNumRSBlocks(numRSBlocks);
             // getNumECBytes = 196 - 66 = 130
             qrCode.SetNumECBytes(numEcBytes);
             // matrix width = 21 + 6 * 4 = 45
             qrCode.SetMatrixWidth(version.GetDimensionForVersion());
             return;
         }
     }
     throw new WriterException("Cannot find proper rs block info (input data too big?)");
 }
예제 #4
0
 /// <summary>Set the error correction level of th QR code.</summary>
 /// <remarks>
 /// Set the error correction level of th QR code.
 /// Possible error correction level values ranked from lowest error correction capability to highest: L, M, Q, H
 /// </remarks>
 /// <param name="value">new error correction level</param>
 public void SetECLevel(ErrorCorrectionLevel value)
 {
     ecLevel = value;
 }
예제 #5
0
 /// <summary>Encode "bytes" with the error correction level "ecLevel".</summary>
 /// <remarks>
 /// Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen
 /// internally by chooseMode(). On success, store the result in "qrCode".
 /// We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for
 /// "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very
 /// strong error correction for this purpose.
 /// Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode()
 /// with which clients can specify the encoding mode. For now, we don't need the functionality.
 /// </remarks>
 /// <exception cref="iText.Barcodes.Qrcode.WriterException"/>
 public static void Encode(String content, ErrorCorrectionLevel ecLevel, QRCode qrCode)
 {
     Encode(content, ecLevel, null, qrCode);
 }
예제 #6
0
        /// <summary>Encode "bytes" with the error correction level "ecLevel".</summary>
        /// <remarks>
        /// Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen
        /// internally by chooseMode(). On success, store the result in "qrCode".
        /// <p>
        /// We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for
        /// "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very
        /// strong error correction for this purpose.
        /// <p>
        /// Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode()
        /// with which clients can specify the encoding mode. For now, we don't need the functionality.
        /// </remarks>
        /// <param name="content">String to encode</param>
        /// <param name="ecLevel">Error-correction level to use</param>
        /// <param name="hints">Optional Map containing  encoding and suggested minimum version to use</param>
        /// <param name="qrCode">QR code to store the result in</param>
        /// <exception cref="WriterException"/>
        /// <exception cref="iText.Barcodes.Qrcode.WriterException"/>
        public static void Encode(String content, ErrorCorrectionLevel ecLevel, IDictionary <EncodeHintType, Object
                                                                                             > hints, QRCode qrCode)
        {
            String encoding = hints == null ? null : (String)hints.Get(EncodeHintType.CHARACTER_SET);

            if (encoding == null)
            {
                encoding = DEFAULT_BYTE_MODE_ENCODING;
            }
            int desiredMinVersion = (hints == null || hints.Get(EncodeHintType.MIN_VERSION_NR) == null) ? 1 : (int)hints
                                    .Get(EncodeHintType.MIN_VERSION_NR);

            //Check if desired level is within bounds of [1,40]
            if (desiredMinVersion < 1)
            {
                desiredMinVersion = 1;
            }
            if (desiredMinVersion > 40)
            {
                desiredMinVersion = 40;
            }
            // 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, desiredMinVersion, 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());
            }
        }