blackMatrix property

  1. @override
BitMatrix blackMatrix
override

Get the 2D array of bits for the image (true means black).

As above, assume this method is expensive and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or may not apply sharpening. Therefore, a row from this matrix may not be identical to one fetched using getBlackRow(), so don't mix and match between them. throws NotFoundException if image can't be binarized to make a matrix

Implementation

@override
BitMatrix get blackMatrix {
  final source = luminanceSource;
  final width = source.width;
  final height = source.height;
  final matrix = BitMatrix(width, height);

  // Quickly calculates the histogram by sampling four rows from the image. This proved to be
  // more robust on the blackbox tests than sampling a diagonal as we used to do.
  _initArrays(width);
  final localBuckets = _buckets;
  for (int y = 1; y < 5; y++) {
    final row = height * y ~/ 5;
    final localLuminances = source.getRow(row, _luminances);
    final right = (width * 4) ~/ 5;
    for (int x = width ~/ 5; x < right; x++) {
      final pixel = localLuminances[x];
      localBuckets[pixel >> _luminanceShift]++;
    }
  }
  final blackPoint = _estimateBlackPoint(localBuckets);

  // We delay reading the entire image luminance until the black point estimation succeeds.
  // Although we end up reading four rows twice, it is consistent with our motto of
  // "fail quickly" which is necessary for continuous scanning.
  final localLuminances = source.matrix;
  for (int y = 0; y < height; y++) {
    final offset = y * width;
    for (int x = 0; x < width; x++) {
      final pixel = localLuminances[offset + x];
      if (pixel < blackPoint) {
        matrix.set(x, y);
      }
    }
  }

  return matrix;
}