예제 #1
0
 public QRCode()
 {
     mode          = null;
     ecLevel       = null;
     version       = -1;
     matrixWidth   = -1;
     maskPattern   = -1;
     numTotalBytes = -1;
     numDataBytes  = -1;
     numECBytes    = -1;
     numRSBlocks   = -1;
     matrix        = null;
 }
예제 #2
0
파일: Encoder.cs 프로젝트: arsalan0312/Eco
        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
파일: Encoder.cs 프로젝트: arsalan0312/Eco
        /**
         * Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
         * modify "qrCode".
         */
        private static void InitQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode,
                                       QRCode qrCode)
        {
            qrCode.SetECLevel(ecLevel);
            qrCode.SetMode(mode);

            // In the following comments, we use numbers of Version 7-H.
            for (int versionNum = 1; 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
        // Make bit vector of type information. On success, store the result in "bits" and return true.
        // Encode error correction level and mask pattern. See 8.9 of
        // JISX0510:2004 (p.45) for details.
        public static void MakeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitVector bits)
        {
            if (!QRCode.IsValidMaskPattern(maskPattern))
            {
                throw new WriterException("Invalid mask pattern");
            }
            int typeInfo = (ecLevel.GetBits() << 3) | maskPattern;

            bits.AppendBits(typeInfo, 5);

            int bchCode = CalculateBCHCode(typeInfo, TYPE_INFO_POLY);

            bits.AppendBits(bchCode, 10);

            BitVector maskBits = new BitVector();

            maskBits.AppendBits(TYPE_INFO_MASK_PATTERN, 15);
            bits.Xor(maskBits);

            if (bits.Size() != 15)
            {  // Just in case.
                throw new WriterException("should not happen but we got: " + bits.Size());
            }
        }
예제 #5
0
 public void SetECLevel(ErrorCorrectionLevel value)
 {
     ecLevel = value;
 }
예제 #6
0
파일: Encoder.cs 프로젝트: arsalan0312/Eco
        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());
            }
        }
예제 #7
0
파일: Encoder.cs 프로젝트: arsalan0312/Eco
 /**
  *  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.
  */
 public static void Encode(String content, ErrorCorrectionLevel ecLevel, QRCode qrCode)
 {
     Encode(content, ecLevel, null, qrCode);
 }
예제 #8
0
 public ECBlocks GetECBlocksForLevel(ErrorCorrectionLevel ecLevel)
 {
     return(ecBlocks[ecLevel.Ordinal()]);
 }