decodeMatrix method

DecoderResult decodeMatrix(
  1. BitMatrix bits
)

Decodes a Data Matrix Code represented as a [BitMatrix]. A 1 or "true" is taken to mean a black module.

@param bits booleans representing white/black Data Matrix Code modules @return text and bytes encoded within the Data Matrix Code @throws FormatException if the Data Matrix Code cannot be decoded @throws ChecksumException if error correction fails

Implementation

DecoderResult decodeMatrix(BitMatrix bits) {
  // Construct a parser and read version, error-correction level
  final parser = BitMatrixParser(bits);
  final version = parser.version;

  // Read codewords
  final codewords = parser.readCodewords();
  // Separate into data blocks
  final dataBlocks = DataBlock.getDataBlocks(codewords, version);

  // Count total number of data bytes
  int totalBytes = 0;
  for (DataBlock db in dataBlocks) {
    totalBytes += db.numDataCodewords;
  }
  final resultBytes = Uint8List(totalBytes);

  final dataBlocksCount = dataBlocks.length;
  // Error-correct and copy data blocks together into a stream of bytes
  for (int j = 0; j < dataBlocksCount; j++) {
    final dataBlock = dataBlocks[j];
    final codewordBytes = dataBlock.codewords;
    final numDataCodewords = dataBlock.numDataCodewords;
    _correctErrors(codewordBytes, numDataCodewords);
    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);
}