Class VisionRect

java.lang.Object
com.codename1.ai.vision.VisionRect

public final class VisionRect extends Object

Immutable normalized rectangle using a top-left origin. X/Y identify the upper-left corner and width/height are fractions of the oriented input dimensions. EMPTY represents unavailable geometry.

toBounds(int, int, int, int) converts one back to pixels for drawing:

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(0x34c759);
    for (Barcode code : lastCodes) {
        Rectangle r = code.getBounds().toBounds(this);
        g.drawRect(r.getX(), r.getY(), r.getWidth(), r.getHeight(), 3);
    }
}
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final VisionRect
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    VisionRect(float x, float y, float width, float height)
    Creates a rectangle in the oriented input image's top-left coordinate space.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
     
    float
     
    float
     
    float
     
    boolean
    Whether this rectangle carries no geometry, which is what a backend that located a result without reporting where reports.
    toBounds(int x, int y, int width, int height)
    Maps this normalized rectangle onto a pixel rectangle.
    Maps this normalized rectangle onto a component's absolute on-screen bounds, which is the coordinate space a paint method draws in.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • VisionRect

      public VisionRect(float x, float y, float width, float height)
      Creates a rectangle in the oriented input image's top-left coordinate space.
      Parameters:
      x - left coordinate
      y - top coordinate
      width - non-negative rectangle width
      height - non-negative rectangle height
  • Method Details

    • getX

      public float getX()
      Returns:
      normalized left coordinate
    • getY

      public float getY()
      Returns:
      normalized top coordinate
    • getWidth

      public float getWidth()
      Returns:
      width as a fraction of oriented input width
    • getHeight

      public float getHeight()
      Returns:
      height as a fraction of oriented input height
    • isEmpty

      public boolean isEmpty()
      Whether this rectangle carries no geometry, which is what a backend that located a result without reporting where reports.
      Returns:
      true when the rectangle has no area
    • toBounds

      public Rectangle toBounds(int x, int y, int width, int height)

      Maps this normalized rectangle onto a pixel rectangle.

      The mapping stretches the whole 0..1 range across width and height. That is correct when the destination has the same aspect ratio as the analyzed image, which is the usual case for an image drawn to fit. A live preview scaled with ScaleType.CROP does not: pass the rectangle the frame actually occupies rather than the component's own bounds.

      Parameters:
      x - left edge of the destination rectangle in pixels
      y - top edge of the destination rectangle in pixels
      width - destination width in pixels
      height - destination height in pixels
      Returns:
      the corresponding pixel rectangle, rounded to whole pixels
    • toBounds

      public Rectangle toBounds(Component target)
      Maps this normalized rectangle onto a component's absolute on-screen bounds, which is the coordinate space a paint method draws in.
      Parameters:
      target - component the analyzed image is displayed in
      Returns:
      the corresponding absolute pixel rectangle
      Throws:
      NullPointerException - if target is null