/// <summary> /// <p>Decodes a Data Matrix Code represented as a <see cref="BitMatrix" />. A 1 or "true" is taken /// to mean a black module.</p> /// </summary> /// <param name="bits">booleans representing white/black Data Matrix Code modules</param> /// <returns>text and bytes encoded within the Data Matrix Code</returns> public DecoderResult decode(BitMatrix bits) { // Construct a parser and read version, error-correction level BitMatrixParser parser = new BitMatrixParser(bits); if (parser.Version == null) { return(null); } // Read codewords byte[] codewords = parser.readCodewords(); if (codewords == null) { return(null); } // Separate into data blocks DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, parser.Version); // Count total number of data bytes int totalBytes = 0; foreach (var db in dataBlocks) { totalBytes += db.NumDataCodewords; } byte[] resultBytes = new byte[totalBytes]; // Error-correct and copy data blocks together into a stream of bytes int dataBlocksCount = dataBlocks.Length; for (int j = 0; j < dataBlocksCount; j++) { DataBlock dataBlock = dataBlocks[j]; byte[] codewordBytes = dataBlock.Codewords; int numDataCodewords = dataBlock.NumDataCodewords; if (!correctErrors(codewordBytes, numDataCodewords)) { return(null); } for (int i = 0; i < numDataCodewords; i++) { // De-interlace data blocks. resultBytes[i * dataBlocksCount + j] = codewordBytes[i]; } } // Decode the contents of that stream of bytes return(DecodedBitStreamParser.decode(resultBytes)); }
/// <summary> /// <p>Decodes a Data Matrix Code represented as a <see cref="BitMatrix" />. A 1 or "true" is taken /// to mean a black module.</p> /// </summary> /// <param name="bits">booleans representing white/black Data Matrix Code modules</param> /// <returns>text and bytes encoded within the Data Matrix Code</returns> public DecoderResult decode(BitMatrix bits) { // Construct a parser and read version, error-correction level BitMatrixParser parser = new BitMatrixParser(bits); if (parser.Version == null) return null; // Read codewords byte[] codewords = parser.readCodewords(); if (codewords == null) return null; // Separate into data blocks DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, parser.Version); int dataBlocksCount = dataBlocks.Length; // Count total number of data bytes int totalBytes = 0; foreach (var db in dataBlocks) { totalBytes += db.NumDataCodewords; } byte[] resultBytes = new byte[totalBytes]; // Error-correct and copy data blocks together into a stream of bytes for (int j = 0; j < dataBlocksCount; j++) { DataBlock dataBlock = dataBlocks[j]; byte[] codewordBytes = dataBlock.Codewords; int numDataCodewords = dataBlock.NumDataCodewords; if (!correctErrors(codewordBytes, numDataCodewords)) return null; for (int i = 0; i < numDataCodewords; i++) { // De-interlace data blocks. resultBytes[i * dataBlocksCount + j] = codewordBytes[i]; } } // Decode the contents of that stream of bytes return DecodedBitStreamParser.decode(resultBytes); }