Reads the bits in the {@link BitMatrix} representing the finder pattern in the correct order in order to reconstruct the codewords bytes contained within the QR Code.
/// <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">The hints.</param> /// <returns> /// text and bytes encoded within the QR Code /// </returns> public DecoderResult decode(BitMatrix bits, IDictionary <DecodeHintType, object> hints) { // Construct a parser and read version, error-correction level BitMatrixParser parser = BitMatrixParser.createBitMatrixParser(bits); if (parser == null) { return(null); } 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, hints)); }
private DecoderResult decode(BitMatrixParser parser, IDictionary<DecodeHintType, object> hints) { 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, hints); }