decode method

BarcodeResult? decode(
  1. BitMatrix matrix
)

Scans the bit matrix to find and decode a barcode.

Tries multiple rows at different positions to find barcodes.

Implementation

BarcodeResult? decode(BitMatrix matrix) {
  final height = matrix.height;
  final width = matrix.width;

  // Try rows at 10%, 30%, 50%, 70%, 90% of height
  final rowPositions = [
    height ~/ 10,
    height * 3 ~/ 10,
    height ~/ 2,
    height * 7 ~/ 10,
    height * 9 ~/ 10,
  ];

  final row = Uint8List(width);

  for (final y in rowPositions) {
    // Extract row data
    for (var x = 0; x < width; x++) {
      row[x] = matrix.get(x, y) ? 1 : 0;
    }

    final runs = BarcodeScanner.getRunLengths(row);
    final result = decodeRow(
      rowNumber: y,
      width: width,
      runs: runs,
      row: row,
    );
    if (result != null) {
      return result;
    }
  }

  return null;
}