Пример #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>
        /// <returns> text and bytes encoded within the QR Code
        /// </returns>
        /// <throws>  ReaderException if the QR Code cannot be decoded </throws>
        public DecoderResult Decode(BitMatrix bits)
        {
            // Construct a parser and read version, error-correction level
            BitMatrixParser      parser  = new BitMatrixParser(bits);
            Version              version = parser.ReadVersion();
            ErrorCorrectionLevel ecLevel = parser.ReadFormatInformation().ErrorCorrectionLevel;

            // Read codewords
            sbyte[] codewords = parser.ReadCodewords();
            // Separate into data blocks
            DataBlock[] dataBlocks = DataBlock.GetDataBlocks(codewords, version, ecLevel);

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

            foreach (DataBlock t in dataBlocks)
            {
                totalBytes += t.NumDataCodewords;
            }
            sbyte[] resultBytes  = new sbyte[totalBytes];
            int     resultOffset = 0;

            // Error-correct and copy data blocks together into a stream of bytes
            foreach (DataBlock dataBlock in dataBlocks)
            {
                sbyte[] codewordBytes    = dataBlock.Codewords;
                int     numDataCodewords = dataBlock.NumDataCodewords;
                CorrectErrors(codewordBytes, numDataCodewords);
                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));
        }