blackWhiteRange method

List<int>? blackWhiteRange(
  1. int fixedDimension,
  2. int maxWhiteRun,
  3. int minDim,
  4. int maxDim,
  5. bool horizontal,
)

Computes the start and end of a region of pixels, either horizontally or vertically, that could be part of a Data Matrix barcode.

@param fixedDimension if scanning horizontally, this is the row (the fixed vertical location) where we are scanning. If scanning vertically it's the column, the fixed horizontal location @param maxWhiteRun largest run of white pixels that can still be considered part of the barcode region @param minDim minimum pixel location, horizontally or vertically, to consider @param maxDim maximum pixel location, horizontally or vertically, to consider @param horizontal if true, we're scanning left-right, instead of up-down @return List

Implementation

List<int>? blackWhiteRange(
  int fixedDimension,
  int maxWhiteRun,
  int minDim,
  int maxDim,
  bool horizontal,
) {
  final center = (minDim + maxDim) ~/ 2;

  // Scan left/up first
  int start = center;
  while (start >= minDim) {
    if (horizontal
        ? _image.get(start, fixedDimension)
        : _image.get(fixedDimension, start)) {
      start--;
    } else {
      final whiteRunStart = start;
      do {
        start--;
      } while (start >= minDim &&
          !(horizontal
              ? _image.get(start, fixedDimension)
              : _image.get(fixedDimension, start)));
      final whiteRunSize = whiteRunStart - start;
      if (start < minDim || whiteRunSize > maxWhiteRun) {
        start = whiteRunStart;
        break;
      }
    }
  }
  start++;

  // Then try right/down
  int end = center;
  while (end < maxDim) {
    if (horizontal
        ? _image.get(end, fixedDimension)
        : _image.get(fixedDimension, end)) {
      end++;
    } else {
      final whiteRunStart = end;
      do {
        end++;
      } while (end < maxDim &&
          !(horizontal
              ? _image.get(end, fixedDimension)
              : _image.get(fixedDimension, end)));
      final whiteRunSize = end - whiteRunStart;
      if (end >= maxDim || whiteRunSize > maxWhiteRun) {
        end = whiteRunStart;
        break;
      }
    }
  }
  end--;

  return end > start ? [start, end] : null;
}