MagicWandTool

Inheritance: java.lang.Object

All Implemented Interfaces: com.aspose.imaging.IPartialArgb32PixelLoader

public class MagicWandTool implements IPartialArgb32PixelLoader

Magic Wand 算法主逻辑的类。

方法

方法描述
select(RasterImage source, MagicWandSettings settings)基于 MagicWandSettings 和源 RasterImage 创建一个新的 ImageBitMask
process(Rectangle pixelsRectangle, int[] pixels, Point start, Point end)处理已加载的像素。

Example: The example shows how to select a simple area of an image based on tone and color of any pixel using Magic Wand tool.

String imageFilePath = "input.png";
String outputFilePath = "masked.png";
try (RasterImage image = (RasterImage)Image.load(imageFilePath))
{
    // 使用魔棒工具基于像素 (120, 100) 的色调和颜色创建新掩码,自定义阈值为 150。
    MagicWandTool
            .select(image, new MagicWandSettings(120, 100) {{ setThreshold(150); }})
            // 将掩码应用于图像
            .apply();

    // 保存图像并强制使用透明颜色类型选项
    image.save(outputFilePath, new PngOptions()
    {{
        setColorType(PngColorType.TruecolorWithAlpha);
    }});
}

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))
{
    // 使用魔棒工具基于像素 (845, 128) 的色调和颜色创建新掩码
    MagicWandTool.select(image, new MagicWandSettings(845, 128))
            // 将现有掩码与由魔棒工具创建的指定掩码合并
            .union(new MagicWandSettings(416, 387))
            // 反转现有掩码
            .invert()
            // 从现有掩码中减去由魔棒工具创建的、具有指定阈值的指定掩码
            .subtract(new MagicWandSettings(1482, 346) {{ setThreshold(69); }})
            // 逐个从现有掩码中减去四个指定的矩形掩码
            .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))
            // 使用指定设置羽化掩码
            .getFeathered(new FeatheringSettings() {{ setSize(3); }})
            // 将掩码应用于图像
            .apply();

    // 保存图像
    image.save(outputFilePath);
}

select(RasterImage source, MagicWandSettings settings)

public static ImageBitMask select(RasterImage source, MagicWandSettings settings)

基于 MagicWandSettings 和源 RasterImage 创建一个新的 ImageBitMask

Parameters:

参数类型描述
sourceRasterImage用于算法处理的光栅图像。
settingsMagicWandSettings用于创建掩码的魔棒算法设置。

Returns: ImageBitMask - New ImageBitMask.

process(Rectangle pixelsRectangle, int[] pixels, Point start, Point end)

public final void process(Rectangle pixelsRectangle, int[] pixels, Point start, Point end)

处理已加载的像素。

Parameters:

参数类型描述
pixelsRectangleRectangle像素矩形。
像素int[]32 位 ARGB 像素。
startPoint起始像素点。如果不等于 (left,top),则表示我们拥有的不是完整矩形。
endPoint结束像素点。如果不等于 (right,bottom),则表示我们拥有的不是完整矩形。