Exemplo n.º 1
0
        /**
         * <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>
         *
         * @param bits booleans representing white/black QR Code modules
         * @return text and bytes encoded within the QR Code
         * @throws ReaderException if the QR Code cannot be decoded
         */
        public DecoderResult decode(BitMatrix bits)
        {
            try{
                // Construct a parser and read version, error-correction level
                BitMatrixParser      parser  = new BitMatrixParser(bits);
                Version              version = parser.readVersion();
                ErrorCorrectionLevel ecLevel = parser.readFormatInformation().getErrorCorrectionLevel();

                // 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;
                for (int i = 0; i < dataBlocks.Length; i++)
                {
                    totalBytes += dataBlocks[i].NumDataCodewords;
                }
                sbyte[] resultBytes  = new sbyte[totalBytes];
                int     resultOffset = 0;

                // Error-correct and copy data blocks together into a stream of bytes
                for (int j = 0; j < dataBlocks.Length; j++)
                {
                    DataBlock dataBlock        = dataBlocks[j];
                    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
                string sResult = DecodedBitStreamParser.decode(resultBytes, version);
                return(new DecoderResult(resultBytes, sResult, null));
            }catch (Exception e) {
                throw new ReaderException(e.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// <p>Decodes a QR Code represented as a <seealso cref="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>
        /// <exception cref="FormatException"> if the QR Code cannot be decoded </exception>
        /// <exception cref="ChecksumException"> if error correction fails </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public com.google.zxing.common.DecoderResult decode(com.google.zxing.common.BitMatrix bits, java.util.Map<com.google.zxing.DecodeHintType,?> hints) throws com.google.zxing.FormatException, com.google.zxing.ChecksumException
        public DecoderResult decode(BitMatrix bits, IDictionary <DecodeHintType, object> hints)
        {
            // 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 dataBlock in dataBlocks)
            {
                totalBytes += dataBlock.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, hints));
        }