Esempio n. 1
0
        /// <summary>
        ///   <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>
        /// </summary>
        /// <param name="bits">booleans representing white/black QR Code modules</param>
        /// <param name="hints">decoding hints that should be used to influence decoding</param>
        /// <returns>
        /// text and bytes encoded within the QR Code
        /// </returns>
        public DecoderResult Decode(BitMatrix bits)
        {
            // Construct a parser and read version, error-correction level
            var parser = BitMatrixParser.CreateBitMatrixParser(bits);

            if (parser == null)
            {
                return(null);
            }

            var result = Decode(parser);

            if (result == null)
            {
                // Revert the bit matrix
                parser.Remask();

                // Will be attempting a mirrored reading of the version and format info.
                parser.SetMirror(true);

                // Preemptively read the version.
                var version = parser.ReadVersion();
                if (version == null)
                {
                    return(null);
                }

                // Preemptively read the format information.
                var formatinfo = parser.ReadFormatInformation();
                if (formatinfo == null)
                {
                    return(null);
                }

                /*
                 * Since we're here, this means we have successfully detected some kind
                 * of version and format information when mirrored. This is a good sign,
                 * that the QR code may be mirrored, and we should try once more with a
                 * mirrored content.
                 */
                // Prepare for a mirrored reading.
                parser.Mirror();

                result = Decode(parser);

                if (result != null)
                {
                    // Success! Notify the caller that the code was mirrored.
                    result.Other = new QRCodeDecoderMetaData(true);
                }
            }

            return(result);
        }
Esempio n. 2
0
        private DecoderResult Decode(BitMatrixParser parser)
        {
            Version version = parser.ReadVersion();

            if (version == null)
            {
                return(null);
            }
            var formatinfo = parser.ReadFormatInformation();

            if (formatinfo == null)
            {
                return(null);
            }
            ErrorCorrectionLevel ecLevel = formatinfo.ErrorCorrectionLevel;

            // Read codewords
            byte[] codewords = parser.ReadCodewords();
            if (codewords == null)
            {
                return(null);
            }
            // Separate into data blocks
            DataBlock[] dataBlocks = DataBlock.GetDataBlocks(codewords, version, ecLevel);

            // Count total number of data bytes
            int totalBytes = 0;

            foreach (var dataBlock in dataBlocks)
            {
                totalBytes += dataBlock.NumDataCodewords;
            }
            byte[] resultBytes  = new byte[totalBytes];
            int    resultOffset = 0;

            // Error-correct and copy data blocks together into a stream of bytes
            foreach (var dataBlock in dataBlocks)
            {
                byte[] codewordBytes    = dataBlock.Codewords;
                int    numDataCodewords = dataBlock.NumDataCodewords;
                if (!CorrectErrors(codewordBytes, numDataCodewords))
                {
                    return(null);
                }
                for (int i = 0; i < numDataCodewords; i++)
                {
                    resultBytes[resultOffset++] = codewordBytes[i];
                }
            }

            // Decode the contents of that stream of bytes
            return(DecodedBitStreamParser.Decode(resultBytes, version, ecLevel));
        }