getBlackMatrix method

  1. @override
BitMatrix getBlackMatrix()
override

Converts a 2D array of luminance data to 1 bit data. 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.

@return The 2D array of bits for the image (true means black). @throws NotFoundException if image can't be binarized to make a matrix

Implementation

@override
BitMatrix getBlackMatrix() {
  var source = luminanceSource;
  var width = source.width;
  var height = source.height;
  var 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);
  var localBuckets = _buckets;
  for (var y = 1; y < 5; y++) {
    var row = height * y ~/ 5;
    var localLuminances = source.getRow(row, _luminances);
    var right = (width * 4) ~/ 5;
    for (var x = width ~/ 5; x < right; x++) {
      var pixel = localLuminances[x] & 0xff;
      localBuckets[pixel >> _luminanceShift]++;
    }
  }
  var 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.
  var localLuminances = source.getMatrix();
  for (var y = 0; y < height; y++) {
    var offset = y * width;
    for (var x = 0; x < width; x++) {
      var pixel = localLuminances[offset + x] & 0xff;
      if (pixel < blackPoint) {
        matrix.set(x, y);
      }
    }
  }

  return matrix;
}