示例#1
0
        // Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
        // pattern conditions.
        public static bool getDataMaskBit(int maskPattern, int x, int y)
        {
            if (!MicroQRCode.isValidMaskPattern(maskPattern))
            {
                throw new System.ArgumentException("Invalid mask pattern");
            }
            int intermediate, temp;

            switch (maskPattern)
            {
            //ISO/IEC 18004:2006(E)  6.8.1 Data mask patterns / Table 10 ¡ª Data mask pattern generation conditions
            case 0:
                intermediate = y & 0x1;
                break;

            case 1:
                intermediate = ((SupportClass.URShift(y, 1)) + (x / 3)) & 0x1;
                break;

            case 2:
                temp         = y * x;
                intermediate = (((temp & 0x1) + (temp % 3)) & 0x1);
                break;

            case 3:
                temp         = y * x;
                intermediate = (((temp % 3) + ((y + x) & 0x1)) & 0x1);
                break;

            default:
                throw new System.ArgumentException("Invalid mask pattern: " + maskPattern);
            }
            return(intermediate == 0);
        }
示例#2
0
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="ecLevel"></param>
        /// <param name="maskPattern"></param>
        /// <param name="bits"></param>
        /// <remarks>
        /// ISO/IEC 18004:2006(E)  6.9 Format information
        /// </remarks>
        public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int version, int maskPattern, BitVector bits)
        {
            if (!MicroQRCode.isValidMaskPattern(maskPattern))
            {
                throw new WriterException("Invalid mask pattern");
            }
            //ISO/IEC 18004:2006(E)  6.9 Format information
            //Symbol number 0:         000
            //Data mask pattern reference:       11
            //Data bits (symbol number, data mask pattern reference):  00011
            //BCH bits:           1101011001
            //Unmasked bit sequence:        000111101011001
            //Mask pattern for XOR operation:       100010001000101
            //Format information module pattern:      100101100011100
            int typeInfo = SYMBOL_NUMBERS_INFO[version - 1][ecLevel.ordinal()];

            if (typeInfo == -1)
            {
                throw new WriterException("Invalid Version&Level info");
            }

            typeInfo = (typeInfo << 2) | 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());
            }
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="content"></param>
        /// <param name="ecLevel"></param>
        /// <param name="hints"></param>
        /// <param name="qrCode"></param>
        public static void encode(System.String content, ErrorCorrectionLevel ecLevel,
                                  System.Collections.Hashtable hints, MicroQRCode qrCode, int versionNum)
        {
            if (versionNum < 1 || versionNum > 4)
            {
                throw new ArgumentOutOfRangeException("versionNum", "versionNum [1, 4]");
            }

            System.String encoding = hints == null ? null : (System.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, versionNum);
            // Step 3: Initialize QR code that can contain "dataBits".
            int numInputBytes = dataBits.sizeInBytes();

            initQRCode(numInputBytes, ecLevel, mode, qrCode, versionNum);

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

            //INFO ECB+Mode+Length+Data[+terminate]
            // Step 4.5: Append ECI message if applicable
            appendModeInfo(mode, headerAndDataBits, versionNum);
            int numLetters = mode.Equals(Mode.BYTE) ? dataBits.sizeInBytes() : content.Length;

            appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits);
            headerAndDataBits.appendBitVector(dataBits);

            // Step 5: Terminate the bits properly.
            terminateBits(qrCode.NumDataBytes, headerAndDataBits, versionNum);

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

            interleaveWithECBytes(headerAndDataBits, versionNum, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits);

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

            qrCode.MaskPattern = chooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix);

            // Step 8.  Build the matrix and set it to "qrCode".
            MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix);
            qrCode.Matrix = matrix;

            //var decoder = new com.google.zxing.microqrcode.decoder.Decoder();
            //var res = decoder.decode(com.google.zxing.common.BitMatrix.FromByteMatrix(matrix));
            //Console.WriteLine(res.Text);

            // Step 9.  Make sure we have a valid QR Code.
            if (!qrCode.Valid)
            {
                throw new WriterException("Invalid QR code: " + qrCode.ToString());
            }
        }
示例#4
0
 /// <summary>  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.
 /// </summary>
 public static void encode(System.String content, ErrorCorrectionLevel ecLevel, MicroQRCode qrCode, int versionNum)
 {
     encode(content, ecLevel, null, qrCode, versionNum);
 }
示例#5
0
        /// <summary> Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
        /// modify "qrCode".
        /// </summary>
        private static void initQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode, MicroQRCode qrCode, int versionNum)
        {
            qrCode.ECLevel = ecLevel;
            qrCode.Mode    = mode;

            // In the following comments, we use numbers of Version 7-H.

            Version version = Version.getVersionForNumber(versionNum);
            // numBytes = 196
            int numBytes = version.TotalCodewords;

            // getNumECBytes = 130
            Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
            int numEcBytes            = ecBlocks.TotalECCodewords;
            // getNumRSBlocks = 5
            int numRSBlocks = ecBlocks.NumBlocks;
            // 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)
            {
                // Yay, we found the proper rs block info!
                qrCode.Version       = versionNum;
                qrCode.NumTotalBytes = numBytes;
                qrCode.NumDataBytes  = numDataBytes;
                qrCode.NumRSBlocks   = numRSBlocks;
                // getNumECBytes = 196 - 66 = 130
                qrCode.NumECBytes = numEcBytes;
                // matrix width = 21 + 6 * 4 = 45
                qrCode.MatrixWidth = version.DimensionForVersion;
                return;
            }
            throw new WriterException("Cannot find proper rs block info (input data too big?)");
        }
示例#6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="ecLevel"></param>
        /// <param name="hints"></param>
        /// <param name="qrCode"></param>
        public static void encode(System.String content, ErrorCorrectionLevel ecLevel,
                                        System.Collections.Hashtable hints, MicroQRCode qrCode, int versionNum)
        {
            if (versionNum < 1 || versionNum > 4)
                throw new ArgumentOutOfRangeException("versionNum", "versionNum [1, 4]");

            System.String encoding = hints == null ? null : (System.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, versionNum);
            // Step 3: Initialize QR code that can contain "dataBits".
            int numInputBytes = dataBits.sizeInBytes();
            initQRCode(numInputBytes, ecLevel, mode, qrCode, versionNum);

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

            //INFO ECB+Mode+Length+Data[+terminate]
            // Step 4.5: Append ECI message if applicable
            appendModeInfo(mode, headerAndDataBits, versionNum);
            int numLetters = mode.Equals(Mode.BYTE) ? dataBits.sizeInBytes() : content.Length;
            appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits);
            headerAndDataBits.appendBitVector(dataBits);

            // Step 5: Terminate the bits properly.
            terminateBits(qrCode.NumDataBytes, headerAndDataBits, versionNum);

            // Step 6: Interleave data bits with error correction code.
            BitVector finalBits = new BitVector();
            interleaveWithECBytes(headerAndDataBits, versionNum, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits);

            // Step 7: Choose the mask pattern and set to "qrCode".
            ByteMatrix matrix = new ByteMatrix(qrCode.MatrixWidth, qrCode.MatrixWidth);
            qrCode.MaskPattern = chooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix);

            // Step 8.  Build the matrix and set it to "qrCode".
            MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix);
            qrCode.Matrix = matrix;

            //var decoder = new com.google.zxing.microqrcode.decoder.Decoder();
            //var res = decoder.decode(com.google.zxing.common.BitMatrix.FromByteMatrix(matrix));
            //Console.WriteLine(res.Text);

            // Step 9.  Make sure we have a valid QR Code.
            if (!qrCode.Valid)
            {
                throw new WriterException("Invalid QR code: " + qrCode.ToString());
            }
        }
示例#7
0
 /// <summary>  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.
 /// </summary>
 public static void encode(System.String content, ErrorCorrectionLevel ecLevel, MicroQRCode qrCode, int versionNum)
 {
     encode(content, ecLevel, null, qrCode, versionNum);
 }
示例#8
0
        /// <summary> Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
        /// modify "qrCode".
        /// </summary>
        private static void initQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode, MicroQRCode qrCode, int versionNum)
        {
            qrCode.ECLevel = ecLevel;
            qrCode.Mode = mode;

            // In the following comments, we use numbers of Version 7-H.

            Version version = Version.getVersionForNumber(versionNum);
            // numBytes = 196
            int numBytes = version.TotalCodewords;
            // getNumECBytes = 130
            Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
            int numEcBytes = ecBlocks.TotalECCodewords;
            // getNumRSBlocks = 5
            int numRSBlocks = ecBlocks.NumBlocks;
            // 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)
            {
                // Yay, we found the proper rs block info!
                qrCode.Version = versionNum;
                qrCode.NumTotalBytes = numBytes;
                qrCode.NumDataBytes = numDataBytes;
                qrCode.NumRSBlocks = numRSBlocks;
                // getNumECBytes = 196 - 66 = 130
                qrCode.NumECBytes = numEcBytes;
                // matrix width = 21 + 6 * 4 = 45
                qrCode.MatrixWidth = version.DimensionForVersion;
                return;
            }
            throw new WriterException("Cannot find proper rs block info (input data too big?)");
        }