RectangleMask

Inheritance: java.lang.Object, com.aspose.imaging.magicwand.imagemasks.ImageMask

public class RectangleMask extends ImageMask

Describes a rectangle mask.

Constructors

ConstructorDescription
RectangleMask(int x, int y, int width, int height)Initializes a new instance of the RectangleMask class with the specified left-top point, width and height.
RectangleMask(Rectangle selectedArea)Initializes a new instance of the RectangleMask class with the specified rectangle.

Methods

MethodDescription
getSelectionBounds()Gets the bounds of the selected part of the mask, in pixels.
get_Item(int x, int y)Gets the opacity of the specified pixel.
inflate(int size)Inflates this mask by the specified amount.
crop(Rectangle rectangle)Crops mask with the specified rectangle.
deepClone()Creates a new object that is a copy of the current instance.

Example: The example shows how to select a complicated area of an image using Magic Wand tool and the ability to interact with masks (invert, union, subtract).

String imageFilePath = "input.png";
String outputFilePath = "masked-complex.png";
try (RasterImage image = (RasterImage)Image.load(imageFilePath))
{
    // Create a new mask using magic wand tool based on tone and color of pixel (845, 128)
    MagicWandTool.select(image, new MagicWandSettings(845, 128))
            // Union the existing mask with the specified one created by magic wand tool
            .union(new MagicWandSettings(416, 387))
            // Invert the existing mask
            .invert()
            // Subtract the specified mask created by magic wand tool with specified threshold from the existing one
            .subtract(new MagicWandSettings(1482, 346) {{ setThreshold(69); }})
            // Subtract four specified rectangle masks from the existing mask one by one
            .subtract(new RectangleMask(0, 0, 800, 150))
            .subtract(new RectangleMask(0, 380, 600, 220))
            .subtract(new RectangleMask(930, 520, 110, 40))
            .subtract(new RectangleMask(1370, 400, 120, 200))
            // Feather mask with specified settings
            .getFeathered(new FeatheringSettings() {{ setSize(3); }})
            // Apply mask to the image
            .apply();

    // Save image
    image.save(outputFilePath);
}

RectangleMask(int x, int y, int width, int height)

public RectangleMask(int x, int y, int width, int height)

Initializes a new instance of the RectangleMask class with the specified left-top point, width and height.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the left-top point of the selected area.
yintThe y-coordinate of the left-top point of the selected area.
widthintWidth of the selected area.
heightintHeight of the selected area.

RectangleMask(Rectangle selectedArea)

public RectangleMask(Rectangle selectedArea)

Initializes a new instance of the RectangleMask class with the specified rectangle.

Parameters:

ParameterTypeDescription
selectedAreaRectangleSelected area specified as a rectangle.

getSelectionBounds()

public Rectangle getSelectionBounds()

Gets the bounds of the selected part of the mask, in pixels.

Returns: Rectangle - the bounds of the selected part of the mask, in pixels.

get_Item(int x, int y)

public boolean get_Item(int x, int y)

Gets the opacity of the specified pixel.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the pixel.
yintThe y-coordinate of the pixel.

Returns: boolean - true if the specified pixel is opaque; otherwise, false.

inflate(int size)

public ImageMask inflate(int size)

Inflates this mask by the specified amount.

Parameters:

ParameterTypeDescription
sizeintThe amount to inflate this mask.

Returns: ImageMask - An inflated RectangleMask as ImageMask.

crop(Rectangle rectangle)

public ImageMask crop(Rectangle rectangle)

Crops mask with the specified rectangle.

Parameters:

ParameterTypeDescription
rectangleRectangleThe specified rectangle.

Returns: ImageMask - A cropped RectangleMask as ImageMask.

deepClone()

public Object deepClone()

Creates a new object that is a copy of the current instance.

Returns: java.lang.Object - A new object that is a copy of this instance.