detect method

List<ResultPoint> detect()

Detects a candidate barcode-like rectangular region within an image. It starts around the center of the image, increases the size of the candidate region until it finds a white rectangular region.

@return ResultPoint describing the corners of the rectangular region. The first and last points are opposed on the diagonal, as are the second and third. The first point will be the topmost point and the last, the bottommost. The second point will be leftmost and the third, the rightmost @throws NotFoundException if no Data Matrix Code can be found

Implementation

List<ResultPoint> detect() {
  int left = _leftInit;
  int right = _rightInit;
  int up = _upInit;
  int down = _downInit;
  bool sizeExceeded = false;
  bool aBlackPointFoundOnBorder = true;

  bool atLeastOneBlackPointFoundOnRight = false;
  bool atLeastOneBlackPointFoundOnBottom = false;
  bool atLeastOneBlackPointFoundOnLeft = false;
  bool atLeastOneBlackPointFoundOnTop = false;

  while (aBlackPointFoundOnBorder) {
    aBlackPointFoundOnBorder = false;

    // .....
    // .   |
    // .....
    bool rightBorderNotWhite = true;
    while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) &&
        right < _width) {
      rightBorderNotWhite = containsBlackPoint(up, down, right, false);
      if (rightBorderNotWhite) {
        right++;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnRight = true;
      } else if (!atLeastOneBlackPointFoundOnRight) {
        right++;
      }
    }

    if (right >= _width) {
      sizeExceeded = true;
      break;
    }

    // .....
    // .   .
    // .___.
    bool bottomBorderNotWhite = true;
    while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) &&
        down < _height) {
      bottomBorderNotWhite = containsBlackPoint(left, right, down, true);
      if (bottomBorderNotWhite) {
        down++;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnBottom = true;
      } else if (!atLeastOneBlackPointFoundOnBottom) {
        down++;
      }
    }

    if (down >= _height) {
      sizeExceeded = true;
      break;
    }

    // .....
    // |   .
    // .....
    bool leftBorderNotWhite = true;
    while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) &&
        left >= 0) {
      leftBorderNotWhite = containsBlackPoint(up, down, left, false);
      if (leftBorderNotWhite) {
        left--;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnLeft = true;
      } else if (!atLeastOneBlackPointFoundOnLeft) {
        left--;
      }
    }

    if (left < 0) {
      sizeExceeded = true;
      break;
    }

    // .___.
    // .   .
    // .....
    bool topBorderNotWhite = true;
    while (
        (topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
      topBorderNotWhite = containsBlackPoint(left, right, up, true);
      if (topBorderNotWhite) {
        up--;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnTop = true;
      } else if (!atLeastOneBlackPointFoundOnTop) {
        up--;
      }
    }

    if (up < 0) {
      sizeExceeded = true;
      break;
    }
  }

  if (!sizeExceeded) {
    final maxSize = right - left;

    ResultPoint? z;
    for (int i = 1; z == null && i < maxSize; i++) {
      z = getBlackPointOnSegment(
        left.toDouble(),
        (down - i).toDouble(),
        (left + i).toDouble(),
        down.toDouble(),
      );
    }

    if (z == null) {
      throw NotFoundException.instance;
    }

    ResultPoint? t;
    //go down right
    for (int i = 1; t == null && i < maxSize; i++) {
      t = getBlackPointOnSegment(
        left.toDouble(),
        (up + i).toDouble(),
        (left + i).toDouble(),
        up.toDouble(),
      );
    }

    if (t == null) {
      throw NotFoundException.instance;
    }

    ResultPoint? x;
    //go down left
    for (int i = 1; x == null && i < maxSize; i++) {
      x = getBlackPointOnSegment(
        right.toDouble(),
        (up + i).toDouble(),
        (right - i).toDouble(),
        up.toDouble(),
      );
    }

    if (x == null) {
      throw NotFoundException.instance;
    }

    ResultPoint? y;
    //go up left
    for (int i = 1; y == null && i < maxSize; i++) {
      y = getBlackPointOnSegment(
        right.toDouble(),
        (down - i).toDouble(),
        (right - i).toDouble(),
        down.toDouble(),
      );
    }

    if (y == null) {
      throw NotFoundException.instance;
    }

    return centerEdges(y, z, x, t);
  } else {
    throw NotFoundException.instance;
  }
}