getBlackRow method

  1. @override
BitArray getBlackRow(
  1. int y,
  2. BitArray? row
)
override

Converts one row of luminance data to 1 bit data. May actually do the conversion, or return cached data. Callers should assume this method is expensive and call it as seldom as possible. This method is intended for decoding 1D barcodes and may choose to apply sharpening. For callers which only examine one row of pixels at a time, the same BitArray should be reused and passed in with each call for performance. However it is legal to keep more than one row at a time if needed.

@param y The row to fetch, which must be in [0, bitmap height) @param row An optional preallocated array. If null or too small, it will be ignored. If used, the Binarizer will call BitArray.clear(). Always use the returned object. @return The array of bits for this row (true means black). @throws NotFoundException if row can't be binarized

Implementation

@override
BitArray getBlackRow(int y, BitArray? row) {
  var source = luminanceSource;
  var width = source.width;
  if (row == null || row.size < width) {
    row = BitArray(width);
  } else {
    row.clear();
  }

  _initArrays(width);
  var localLuminances = source.getRow(y, _luminances);
  var localBuckets = _buckets;
  for (var x = 0; x < width; x++) {
    localBuckets[(localLuminances[x] & 0xff) >> _luminanceShift]++;
  }
  var blackPoint = _estimateBlackPoint(localBuckets);

  if (width < 3) {
    // Special case for very small images
    for (var x = 0; x < width; x++) {
      if ((localLuminances[x] & 0xff) < blackPoint) {
        row.set(x);
      }
    }
  } else {
    var left = localLuminances[0] & 0xff;
    var center = localLuminances[1] & 0xff;
    for (var x = 1; x < width - 1; x++) {
      var right = localLuminances[x + 1] & 0xff;
      // A simple -1 4 -1 box filter with a weight of 2.
      if (((center * 4) - left - right) / 2 < blackPoint) {
        row.set(x);
      }
      left = center;
      center = right;
    }
  }
  return row;
}