图像

Inheritance: java.lang.Object, com.aspose.imaging.DisposableObject, com.aspose.imaging.DataStreamSupporter

All Implemented Interfaces: com.aspose.imaging.IObjectWithBounds, com.aspose.internal.progressmanagement.IProgressInformer, com.aspose.internal.progressmanagement.IProgressEventHandler, com.aspose.imaging.IMetadataContainer

public abstract class Image extends DataStreamSupporter implements IObjectWithBounds, IProgressInformer, IProgressEventHandler, IMetadataContainer

Image 是所有图像类型的基类。

方法

方法描述
canLoad(String filePath)确定是否可以从指定的文件路径加载图像。
canLoad(String filePath, LoadOptions loadOptions)确定是否可以从指定的文件路径加载图像,并可选择使用指定的打开选项。
canLoad(InputStream stream)确定是否可以从指定的流加载图像。
canLoad(InputStream stream, LoadOptions loadOptions)确定是否可以从指定的流加载图像,并可选择使用指定的 loadOptions
create(ImageOptionsBase imageOptions, int width, int height)使用指定的创建选项创建新图像。
create(ImageOptionsBase imageOptions, int width, int height, int[] pixels)从提供的像素数组创建一个 RasterImage 实例。
create(Image[] images)使用指定的图像作为页面创建新图像
create(MultipageCreateOptions multipageCreateOptions)创建指定的多页创建选项。
create(String[] files, boolean throwExceptionOnLoadError)创建包含指定文件的多页图像。
create(String[] files)创建包含指定文件的多页图像。
create(Image[] images, boolean disposeImages)创建一个新图像,将指定的图像作为页面。
getFileFormat(String filePath)获取文件格式。
load(String filePath, LoadOptions loadOptions)从指定的文件路径或 URL 加载新图像。
load(String filePath)从指定的文件路径或 URL 加载新图像。
load(RandomAccessFile file, LoadOptions loadOptions)从指定的流加载新图像。
load(RandomAccessFile file)从指定的流加载新图像。
load(InputStream stream, LoadOptions loadOptions)从指定的流加载新图像。
load(InputStream stream)从指定的流加载新图像。
getFileFormat(InputStream stream)获取文件格式。
getFittingRectangle(Rectangle rectangle, int width, int height)获取适合当前图像的矩形。
getFittingRectangle(Rectangle rectangle, int[] pixels, int width, int height)获取适合当前图像的矩形。
getProportionalWidth(int width, int height, int newHeight)获取比例宽度。
getProportionalHeight(int width, int height, int newWidth)获取比例高度。
removeMetadata()移除元数据。
trySetMetadata(IImageMetadataFormat metadata)尝试设置一个 metadata 实例,如果此 Image 实例支持并实现 IImageMetadataFormat 类型。
getBitsPerPixel()获取图像每像素位数计数。
getBounds()获取图像边界。
getContainer()获取 Image 容器。
getPalette()获取颜色调色板。
setPalette(IColorPalette value)设置颜色调色板。
isUsePalette()获取指示是否使用图像调色板的值。
getSize()获取图像尺寸。
getInterruptMonitor()获取中断监视器。
setInterruptMonitor(InterruptMonitor value)设置中断监视器。
getBufferSizeHint()获取缓冲区大小提示,该提示定义了所有内部缓冲区的最大允许大小。
setBufferSizeHint(int value)设置缓冲区大小提示,该提示定义了所有内部缓冲区的最大允许大小。
isAutoAdjustPalette()获取指示是否自动调整调色板的值。
setAutoAdjustPalette(boolean value)设置指示是否自动调整调色板的值。
hasBackgroundColor()获取指示图像是否具有背景颜色的值。
getFileFormat()使用此用户友好的属性轻松检索文件格式值。
getBackgroundColor()获取或设置背景颜色的值。
setBackgroundColor(boolean value)获取或设置指示图像是否具有背景颜色的值。
setBackgroundColor(Color value)获取或设置背景颜色的值。
getMetadata()获取图像元数据。
getExifData()获取 Exif 数据。
setExifData(ExifData value)设置 Exif 数据。
getXmpData()获取 Xmp 数据。
setXmpData(XmpPacketWrapper value)设置 Xmp 数据。
getIProgressEventHandler()获取进度事件处理程序的信息。
getProgressEventHandlerInfo()获取进度事件处理程序的信息。
canSave(ImageOptionsBase options)确定图像是否可以保存为由传入的保存选项表示的指定文件格式。
resize(int newWidth, int newHeight)调整图像大小。
resize(int newWidth, int newHeight, int resizeType)调整图像大小。
resize(int newWidth, int newHeight, ImageResizeSettings settings)调整图像大小。
getDefaultOptions(Object[] args)获取默认选项。
getOriginalOptions()根据原始文件设置获取选项。
resizeWidthProportionally(int newWidth)按比例调整宽度。
resizeHeightProportionally(int newHeight)按比例调整高度。
resizeWidthProportionally(int newWidth, int resizeType)按比例调整宽度。
resizeHeightProportionally(int newHeight, int resizeType)按比例调整高度。
resizeWidthProportionally(int newWidth, ImageResizeSettings settings)按比例调整宽度。
resizeHeightProportionally(int newHeight, ImageResizeSettings settings)按比例调整高度。
rotateFlip(int rotateFlipType)旋转、翻转,或同时旋转和翻转图像。
rotate(float angle)围绕中心旋转图像。
crop(Rectangle rectangle)裁剪指定的矩形。
crop(int leftShift, int rightShift, int topShift, int bottomShift)使用位移裁剪图像。
save()将图像数据保存到底层流。
save(String filePath)将图像保存到指定的文件位置。
save(String filePath, ImageOptionsBase options)根据保存选项,将对象的数据以指定的文件格式保存到指定的文件位置。
save(String filePath, ImageOptionsBase options, Rectangle boundsRectangle)根据保存选项,将对象的数据以指定的文件格式保存到指定的文件位置。
save(RandomAccessFile file, ImageOptionsBase options)根据保存选项,将对象的数据以指定的文件格式保存到指定的文件位置。
save(RandomAccessFile file, ImageOptionsBase optionsBase, Rectangle boundsRectangle)根据保存选项,将图像的数据以指定的文件格式保存到指定的流中。
save(OutputStream stream, ImageOptionsBase optionsBase)根据保存选项,将图像的数据以指定的文件格式保存到指定的流中。
save(OutputStream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)根据保存选项,将图像的数据以指定的文件格式保存到指定的流中。
setPalette(IColorPalette palette, boolean updateColors)设置图像调色板。
getSerializedStream(ImageOptionsBase imageOptions, Rectangle clippingRectangle, int[] pageNumber)转换为 aps。

Example: This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance.

此示例根据 BmpOptions 实例的 Source 属性在某个磁盘位置创建一个新的 Image 文件。在创建实际图像之前,会设置 BmpOptions 实例的多个属性。特别是 Source 属性,在本例中指向实际的磁盘位置。

// 创建 BmpOptions 的实例并设置其各项属性。
com.aspose.imaging.imageoptions.BmpOptions bmpOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpOptions.setBitsPerPixel(24);

// 创建 FileCreateSource 的实例并将其指定为 BmpOptions 实例的 Source。
// 第二个布尔参数决定要创建的文件是否为 IsTemporal。
bmpOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("C:\\temp\\sample.bmp", false));

// 创建 Image 实例,并通过调用 Create 方法使用 BmpOptions 实例进行初始化。
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpOptions, 500, 500);
try {
    // 进行一些图像处理。

    // 保存所有更改。
    image.save();
} finally {
    image.dispose();
}

Example: Resize image using specific Resize Type.

try (Image image = Image.load("Photo.jpg"))
{
    image.resize(640, 480, ResizeType.CatmullRom);
    image.save("ResizedPhoto.jpg");

    image.resize(1024, 768, ResizeType.CubicConvolution);
    image.save("ResizedPhoto2.jpg");

    ImageResizeSettings resizeSettings = new ImageResizeSettings();
    resizeSettings.setMode(ResizeType.CubicBSpline);
    resizeSettings.setFilterType(ImageFilterType.SmallRectangular);

    image.resize(800, 800, resizeSettings);
    image.save("ResizedPhoto3.jpg");
}

Example: Determine if the palette is used by the image.

try (Image image = Image.load("Sample.bmp"))
{
    if (image.isUsePalette())
    {
        System.out.println("The palette is used by the image");
    }
}

canLoad(String filePath)

public static boolean canLoad(String filePath)

确定是否可以从指定的文件路径加载图像。

Parameters:

参数类型描述
filePathjava.lang.String文件路径。

Returns: 布尔型 - true 表示可以从指定文件加载图像;否则为 false

Example: This example determines whether image can be loaded from a file.


// 使用文件的绝对路径
boolean canLoad = com.aspose.imaging.Image.canLoad("c:\\temp\\sample.gif");

canLoad(String filePath, LoadOptions loadOptions)

public static boolean canLoad(String filePath, LoadOptions loadOptions)

确定是否可以从指定的文件路径加载图像,并可选择使用指定的打开选项。

Parameters:

参数类型描述
filePathjava.lang.String文件路径。
loadOptionsLoadOptions加载选项。

Returns: 布尔型 - true 表示可以从指定文件加载图像;否则为 false

canLoad(InputStream stream)

public static boolean canLoad(InputStream stream)

确定是否可以从指定的流加载图像。

Parameters:

参数类型描述
java.io.InputStream要加载的流。

Returns: 布尔型 - true 表示可以从指定流加载图像;否则为 false

Example: This example determines whether image can be loaded from a file stream.

String dir = "c:\\temp\\";

boolean canLoad;

// 使用文件流。
java.io.InputStream stream = new java.io.FileInputStream(dir + "sample.bmp");
try {
    canLoad = com.aspose.imaging.Image.canLoad(stream);
} finally {
    stream.close();
}

// 以下数据不是有效的图像流,因此 CanLoad 返回 false。
byte[] imageData = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
stream = new java.io.ByteArrayInputStream(imageData);
{
    canLoad = com.aspose.imaging.Image.canLoad(stream);
}

canLoad(InputStream stream, LoadOptions loadOptions)

public static boolean canLoad(InputStream stream, LoadOptions loadOptions)

确定是否可以从指定的流加载图像,并可选择使用指定的 loadOptions

Parameters:

参数类型描述
java.io.InputStream要加载的流。
loadOptionsLoadOptions加载选项。

Returns: 布尔型 - true 表示可以从指定流加载图像;否则为 false

create(ImageOptionsBase imageOptions, int width, int height)

public static Image create(ImageOptionsBase imageOptions, int width, int height)

使用指定的创建选项创建新图像。

Parameters:

参数类型描述
imageOptionsImageOptionsBase图像选项。
宽度int宽度。
高度int高度。

Returns: Image - The newly created image.

Example: This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance. 此示例根据 BmpOptions 实例的 Source 属性在某个磁盘位置创建一个新的 Image 文件。在创建实际图像之前,会设置 BmpOptions 实例的多个属性。特别是 Source 属性,在本例中指向实际的磁盘位置。

// 创建 BmpOptions 的实例并设置其各项属性。
com.aspose.imaging.imageoptions.BmpOptions bmpOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpOptions.setBitsPerPixel(24);

// 创建 FileCreateSource 的实例并将其指定为 BmpOptions 实例的 Source。
// 第二个布尔参数决定要创建的文件是否为 IsTemporal。
bmpOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("C:\\temp\\sample.bmp", false));

// 创建 Image 实例,并通过调用 Create 方法使用 BmpOptions 实例进行初始化。
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpOptions, 500, 500);
try {
    // 进行一些图像处理。

    // 保存所有更改。
    image.save();
} finally {
    image.dispose();
}

create(ImageOptionsBase imageOptions, int width, int height, int[] pixels)

public static Image create(ImageOptionsBase imageOptions, int width, int height, int[] pixels)

从提供的像素数组创建一个 RasterImage 实例。验证指定的宽度和高度是否匹配像素数据的尺寸。此方法只能在库处于授权模式时使用。

Parameters:

参数类型描述
imageOptionsImageOptionsBase用于创建 RasterImage 的选项。
widthintRasterImage 的宽度。
heightintRasterImage 的高度。
像素int[]用于填充图像的像素值数组。

Returns: Image - A RasterImage populated with the provided pixel data.

create(Image[] images)

public static Image create(Image[] images)

使用指定的图像作为页面创建新图像

Parameters:

参数类型描述
imagesImage[]图像。

Returns: Image - The Image as IMultipageImage

create(MultipageCreateOptions multipageCreateOptions)

public static Image create(MultipageCreateOptions multipageCreateOptions)

创建指定的多页创建选项。

Parameters:

参数类型描述
multipageCreateOptionsMultipageCreateOptions多页创建选项。

Returns: Image - The multipage image

create(String[] files, boolean throwExceptionOnLoadError)

public static Image create(String[] files, boolean throwExceptionOnLoadError)

创建包含指定文件的多页图像。

Parameters:

参数类型描述
文件java.lang.String[]文件。
throwExceptionOnLoadErrorboolean如果设置为 true [throw exception on load error]。

Returns: Image - The multipage image

create(String[] files)

public static Image create(String[] files)

创建包含指定文件的多页图像。

Parameters:

参数类型描述
文件java.lang.String[]文件。

Returns: Image - The multipage image

create(Image[] images, boolean disposeImages)

public static Image create(Image[] images, boolean disposeImages)

创建一个新图像,将指定的图像作为页面。

Parameters:

参数类型描述
imagesImage[]图像。
disposeImagesboolean如果设置为 true [dispose images]。

Returns: Image - The Image as IMultipageImage

getFileFormat(String filePath)

public static long getFileFormat(String filePath)

获取文件格式。

Parameters:

参数类型描述
filePathjava.lang.String

确定的文件格式并不意味着指定的图像一定可以加载。使用 CanLoad 方法的重载之一来确定文件是否可以加载。 |

Returns: long - 确定的文件格式。

Example: This example shows how to determine the image format without loading the entire image from a file.

String dir = "c:\\temp\\";

// 使用文件的绝对路径
long format = com.aspose.imaging.Image.getFileFormat(dir + "sample.gif");

// 一个文件格式的字符串表示。
String strFormat;
if (format == com.aspose.imaging.FileFormat.Bmp) {
    strFormat = "BMP";
} else if (format == com.aspose.imaging.FileFormat.Gif) {
    strFormat = "GIF";
} else if (format == com.aspose.imaging.FileFormat.Dicom) {
    strFormat = "DICOM";
} else if (format == com.aspose.imaging.FileFormat.Djvu) {
    strFormat = "DJVU";
} else if (format == com.aspose.imaging.FileFormat.Dng) {
    strFormat = "DNG";
} else if (format == com.aspose.imaging.FileFormat.Png) {
    strFormat = "PNG";
} else if (format == com.aspose.imaging.FileFormat.Jpeg) {
    strFormat = "JPEG";
} else if (format == com.aspose.imaging.FileFormat.Jpeg2000) {
    strFormat = "JPEG2000";
} else if (format == com.aspose.imaging.FileFormat.Psd) {
    strFormat = "PSD";
} else if (format == com.aspose.imaging.FileFormat.Tiff) {
    strFormat = "Tiff";
} else if (format == com.aspose.imaging.FileFormat.Webp) {
    strFormat = "WEBP";
} else if (format == com.aspose.imaging.FileFormat.Cdr) {
    strFormat = "CDR";
} else if (format == com.aspose.imaging.FileFormat.Cmx) {
    strFormat = "CMX";
} else if (format == com.aspose.imaging.FileFormat.Emf) {
    strFormat = "EMF";
} else if (format == com.aspose.imaging.FileFormat.Wmf) {
    strFormat = "WMF";
} else if (format == com.aspose.imaging.FileFormat.Svg) {
    strFormat = "SVG";
} else if (format == com.aspose.imaging.FileFormat.Odg) {
    strFormat = "ODG";
} else if (format == com.aspose.imaging.FileFormat.Eps) {
    strFormat = "EPS";
} else {
    strFormat = "UNDEFINED";
}

System.out.println("The file format is " + strFormat);

load(String filePath, LoadOptions loadOptions)

public static Image load(String filePath, LoadOptions loadOptions)

从指定的文件路径或 URL 加载新图像。如果 filePath 是文件路径,方法仅打开该文件。如果 filePath 是 URL,方法会下载该文件,将其存储为临时文件,然后打开它。

Parameters:

参数类型描述
filePathjava.lang.String加载图像的文件路径或 URL。
loadOptionsLoadOptions加载选项。

Returns: Image - The loaded image.

load(String filePath)

public static Image load(String filePath)

从指定的文件路径或 URL 加载新图像。如果 filePath 是文件路径,方法仅打开该文件。如果 filePath 是 URL,方法会下载该文件,将其存储为临时文件,然后打开它。

Parameters:

参数类型描述
filePathjava.lang.String加载图像的文件路径或 URL。

Returns: Image - The loaded image.

Example: This example demonstrates the loading of an existing Image file into an instance of com. 此示例演示使用指定的文件路径将现有 Image 文件加载到 com.aspose.imaging.Image 实例中

// 创建 Image 实例并使用磁盘位置的现有图像文件进行初始化
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp");
try {
    // 进行一些图像处理。
} finally {
    image.dispose();
}

load(RandomAccessFile file, LoadOptions loadOptions)

public static Image load(RandomAccessFile file, LoadOptions loadOptions)

从指定的流加载新图像。

Parameters:

参数类型描述
文件java.io.RandomAccessFile加载图像的文件。
loadOptionsLoadOptions加载选项。

Returns: Image - The loaded image.

load(RandomAccessFile file)

public static Image load(RandomAccessFile file)

从指定的流加载新图像。

Parameters:

参数类型描述
文件java.io.RandomAccessFile加载图像的文件。

Returns: Image - The loaded image.

load(InputStream stream, LoadOptions loadOptions)

public static Image load(InputStream stream, LoadOptions loadOptions)

从指定的流加载新图像。

Parameters:

参数类型描述
java.io.InputStream用于加载图像的流。
loadOptionsLoadOptions加载选项。

Returns: Image - The loaded image.

load(InputStream stream)

public static Image load(InputStream stream)

从指定的流加载新图像。

Parameters:

参数类型描述
java.io.InputStream用于加载图像的流。

Returns: Image - The loaded image.

Example: This example demonstrates the use of InputStream object to load an existing Image file

// 创建 FileInputStream 的实例
java.io.InputStream stream = new java.io.FileInputStream("C:\\temp\\sample.bmp");
try {
    // 创建 Image 类的实例,并通过调用 Load 方法使用 FileStream 对象加载已有文件
    com.aspose.imaging.Image image = com.aspose.imaging.Image.load(stream);
    try {
        // 进行一些图像处理。
    } finally {
        image.dispose();
    }
} finally {
    stream.close();
}

getFileFormat(InputStream stream)

public static long getFileFormat(InputStream stream)

获取文件格式。

Parameters:

参数类型描述
java.io.InputStream

确定的文件格式并不意味着可以加载指定的图像。使用 CanLoad 方法的其中一个重载来确定流是否可以加载。 |

Returns: long - 确定的文件格式。

Example: This example shows how to determine the image format without loading the entire image from a file stream.


// 下面主示例中使用的辅助类。
class Utils {
    // 获取文件格式字符串表示的辅助方法。
    public String getFileFormatString(long fileFormat) {
        if (fileFormat == com.aspose.imaging.FileFormat.Bmp) {
            return "BMP";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Gif) {
            return "GIF";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Dicom) {
            return "DICOM";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Djvu) {
            return "DJVU";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Dng) {
            return "DNG";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Png) {
            return "PNG";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Jpeg) {
            return "JPEG";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Jpeg2000) {
            return "JPEG2000";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Psd) {
            return "PSD";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Tiff) {
            return "Tiff";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Webp) {
            return "WEBP";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Cdr) {
            return "CDR";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Cmx) {
            return "CMX";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Emf) {
            return "EMF";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Wmf) {
            return "WMF";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Svg) {
            return "SVG";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Odg) {
            return "ODG";
        } else if (fileFormat == com.aspose.imaging.FileFormat.Eps) {
            return "EPS";
        } else {
            return "UNDEFINED";
        }
    }
}

// 以下是主要示例
Utils utils = new Utils();

String dir = "c:\\temp\\";

// 使用文件流。
java.io.InputStream stream = new java.io.FileInputStream(dir + "sample.bmp");
{
    long format = com.aspose.imaging.Image.getFileFormat(stream);
    System.out.println("The file format is " + utils.getFileFormatString(format));
}

// 以下数据不是有效的图像流,因此 GetFileFormat 返回 FileFormat.Undefined。
byte[] imageData = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
stream = new java.io.ByteArrayInputStream(imageData);
{
    long format = com.aspose.imaging.Image.getFileFormat(stream);
    System.out.println("The file format is " + utils.getFileFormatString(format));
}

// 输出可能如下所示:
// 文件格式为 BMP
// 文件格式为 UNDEFINED

getFittingRectangle(Rectangle rectangle, int width, int height)

public static Rectangle getFittingRectangle(Rectangle rectangle, int width, int height)

获取适合当前图像的矩形。

Parameters:

参数类型描述
rectangleRectangle用于获取适配矩形的矩形。
宽度int对象的宽度。
高度int对象的高度。

Returns: Rectangle - The fitting rectangle or exception if no fitting rectangle can be found.

getFittingRectangle(Rectangle rectangle, int[] pixels, int width, int height)

public static Rectangle getFittingRectangle(Rectangle rectangle, int[] pixels, int width, int height)

获取适合当前图像的矩形。

Parameters:

参数类型描述
rectangleRectangle用于获取适配矩形的矩形。
像素int[]32 位 ARGB 像素。
宽度int对象的宽度。
高度int对象的高度。

Returns: Rectangle - The fitting rectangle or exception if no fitting rectangle can be found.

getProportionalWidth(int width, int height, int newHeight)

public static int getProportionalWidth(int width, int height, int newHeight)

获取比例宽度。

Parameters:

参数类型描述
宽度int宽度。
高度int高度。
newHeightint新的高度。

Returns: int - 比例宽度。

getProportionalHeight(int width, int height, int newWidth)

public static int getProportionalHeight(int width, int height, int newWidth)

获取比例高度。

Parameters:

参数类型描述
宽度int宽度。
高度int高度。
newWidthint新的宽度。

Returns: int - 比例高度。

removeMetadata()

public void removeMetadata()

移除元数据。

trySetMetadata(IImageMetadataFormat metadata)

public boolean trySetMetadata(IImageMetadataFormat metadata)

尝试设置一个 metadata 实例,如果此 Image 实例支持并实现 IImageMetadataFormat 类型。

Parameters:

参数类型描述
metadataIImageMetadataFormat元数据。

Returns: boolean - 如果 Image 实例支持并实现 IImageMetadataFormat 类型,则为 True;否则为 false。

getBitsPerPixel()

public abstract int getBitsPerPixel()

获取图像每像素位数计数。

Returns: int - 图像每像素位数。

getBounds()

public Rectangle getBounds()

获取图像边界。

Returns: Rectangle - The image bounds.

getContainer()

public Image getContainer()

获取 Image 容器。

值:Image 容器。

如果此属性不为 null,则表示该图像包含在另一个图像中。

Returns: Image

getPalette()

public IColorPalette getPalette()

获取颜色调色板。当像素直接表示时,不使用颜色调色板。

Returns: IColorPalette - The color palette.

setPalette(IColorPalette value)

public void setPalette(IColorPalette value)

设置颜色调色板。当像素直接表示时,不使用颜色调色板。

Parameters:

参数类型描述
valueIColorPalette颜色调色板。

isUsePalette()

public boolean isUsePalette()

获取指示是否使用图像调色板的值。

值:true 表示在图像中使用调色板;否则为 false

Returns: boolean - 表示是否使用图像调色板的值。

Example: Determine if the palette is used by the image.

try (Image image = Image.load("Sample.bmp"))
{
    if (image.isUsePalette())
    {
        System.out.println("The palette is used by the image");
    }
}

getSize()

public Size getSize()

获取图像尺寸。

Returns: Size - The image size.

Example: This example shows how to load a DJVU image from a file stream and print information about the pages.

String dir = "c:\\temp\\";

// 从文件流加载 DJVU 图像。
java.io.FileInputStream stream = new java.io.FileInputStream(dir + "sample.djvu");
try {
    com.aspose.imaging.fileformats.djvu.DjvuImage djvuImage = new com.aspose.imaging.fileformats.djvu.DjvuImage(stream);
    try {
        System.out.println("The total number of pages: " + djvuImage.getPages().length);
        System.out.println("The active page number:    " + djvuImage.getActivePage().getPageNumber());
        System.out.println("The first page number:     " + djvuImage.getFirstPage().getPageNumber());
        System.out.println("The last page number:      " + djvuImage.getLastPage().getPageNumber());

        for (com.aspose.imaging.fileformats.djvu.DjvuPage djvuPage : djvuImage.getPages()) {
            System.out.println("--------------------------------------------------");
            System.out.println("Page number:     " + djvuPage.getPageNumber());
            System.out.println("Page size:       " + djvuPage.getSize());
            System.out.println("Page raw format: " + djvuPage.getRawDataFormat());
        }
    } finally {
        djvuImage.dispose();
    }
} finally {
    stream.close();
}

//输出可能如下所示:
//总页数:2
//当前页码:    1
//第一页号:     1
//最后页码:      2
//--------------------------------------------------
//页码:     1
//页面大小:       { Width = 2481, Height = 3508}
//页面原始格式:RgbIndexed1Bpp,使用的通道: 1
//--------------------------------------------------
//页码:     2
//页面大小:       { Width = 2481, Height = 3508}
//页面原始格式:RgbIndexed1Bpp,使用的通道: 1

getInterruptMonitor()

public InterruptMonitor getInterruptMonitor()

获取中断监视器。

Returns: InterruptMonitor - the interrupt monitor.

setInterruptMonitor(InterruptMonitor value)

public void setInterruptMonitor(InterruptMonitor value)

设置中断监视器。

Parameters:

参数类型描述
valueInterruptMonitor中断监视器。

getBufferSizeHint()

public final int getBufferSizeHint()

获取缓冲区大小提示,该提示定义了所有内部缓冲区的最大允许大小。

值:缓冲区大小提示(单位:兆字节)。非正值表示内部缓冲区没有内存限制

Returns: int - 缓冲区大小提示,定义为所有内部缓冲区的最大允许大小。

setBufferSizeHint(int value)

public final void setBufferSizeHint(int value)

设置缓冲区大小提示,该提示定义了所有内部缓冲区的最大允许大小。

值:缓冲区大小提示(单位:兆字节)。非正值表示内部缓冲区没有内存限制

Parameters:

参数类型描述
valueint缓冲区大小提示,定义为所有内部缓冲区的最大允许大小。

isAutoAdjustPalette()

public boolean isAutoAdjustPalette()

获取指示是否自动调整调色板的值。

Returns: boolean - true 表示启用自动调整调色板;否则为 false

setAutoAdjustPalette(boolean value)

public void setAutoAdjustPalette(boolean value)

设置指示是否自动调整调色板的值。

Parameters:

参数类型描述
valuebooleantrue 表示启用自动调整调色板;否则为 false

hasBackgroundColor()

public boolean hasBackgroundColor()

获取指示图像是否具有背景颜色的值。

Returns: boolean

getFileFormat()

public long getFileFormat()

使用此用户友好的属性可以轻松获取文件格式值。对于希望快速获取文件格式信息的开发者而言,这是理想的选择。

Returns: long

getBackgroundColor()

public Color getBackgroundColor()

获取或设置背景颜色的值。

Returns: Color

setBackgroundColor(boolean value)

public void setBackgroundColor(boolean value)

获取或设置指示图像是否具有背景颜色的值。

Parameters:

参数类型描述
valueboolean

setBackgroundColor(Color value)

public void setBackgroundColor(Color value)

获取或设置背景颜色的值。

Parameters:

参数类型描述
valueColor

getMetadata()

public ImageMetadata getMetadata()

获取图像元数据。

Returns: ImageMetadata - the image metadata.

getExifData()

public ExifData getExifData()

获取 Exif 数据。

Returns: ExifData - the Exif data.

setExifData(ExifData value)

public void setExifData(ExifData value)

设置 Exif 数据。

Parameters:

参数类型描述
valueExifDataExif 数据。

getXmpData()

public final XmpPacketWrapper getXmpData()

获取 Xmp 数据。

Returns: XmpPacketWrapper - the Xmp data.

setXmpData(XmpPacketWrapper value)

public final void setXmpData(XmpPacketWrapper value)

设置 Xmp 数据。

Parameters:

参数类型描述
valueXmpPacketWrapperXmp 数据。

getIProgressEventHandler()

public final ProgressEventHandler getIProgressEventHandler()

获取进度事件处理程序的信息。

Returns: ProgressEventHandler - the progress event handler information.

getProgressEventHandlerInfo()

public final ProgressEventHandlerInfo getProgressEventHandlerInfo()

获取进度事件处理程序的信息。

值:进度事件处理程序信息。

Returns: ProgressEventHandlerInfo - the progress event handler information.

canSave(ImageOptionsBase options)

public boolean canSave(ImageOptionsBase options)

确定图像是否可以保存为由传入的保存选项表示的指定文件格式。

Parameters:

参数类型描述
optionsImageOptionsBase要使用的保存选项。

Returns: boolean - true 表示图像可以保存为传入保存选项所表示的指定文件格式;否则为 false

Example: This example shows how to determine whether image can be saved to the specified file format represented by the passed save options.

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    com.aspose.imaging.imageoptions.JpegOptions saveOptions = new com.aspose.imaging.imageoptions.JpegOptions();
    saveOptions.setQuality(50);

    // 确定图像是否可以保存为 JPEG
    boolean canSave = image.canSave(saveOptions);
} finally {
    image.dispose();
}

resize(int newWidth, int newHeight)

public void resize(int newWidth, int newHeight)

调整图像大小。默认使用 ResizeType.NearestNeighbourResample

Parameters:

参数类型描述
newWidthint新的宽度。
newHeightint新的高度。

Example: The following example shows how to resize a metafile (WMF and EMF).

String baseFolder = "c:\\temp\\";

String[] files = new String[]{"image3.emf", "image4.wmf"};
for (String fileName : files) {
    String inputFile = baseFolder + fileName;
    String outputFile = baseFolder + "Resize_" + fileName;
    com.aspose.imaging.fileformats.emf.MetaImage image = (com.aspose.imaging.fileformats.emf.MetaImage) com.aspose.imaging.Image.load(inputFile);
    try {
        image.resize(image.getWidth() / 4, image.getHeight() / 4);
        image.save(outputFile);
    } finally {
        image.close();
    }
}

Example: The following example shows how to resize SVG image and save it to PNG.

String dir = "c:\\aspose.imaging\\java\\issues\\1431\\";
String[] fileNames = new String[] {
                "Logotype.svg",
                "sample_car.svg",
                "rg1024_green_grapes.svg",
                "MidMarkerFigure.svg",
                "embeddedFonts.svg"
        };

com.aspose.imaging.PointF[] scales = new com.aspose.imaging.PointF[] {
                new com.aspose.imaging.PointF(0.5f, 0.5f),
                new com.aspose.imaging.PointF(1f, 1f),
                new com.aspose.imaging.PointF(2f, 2f),
                new com.aspose.imaging.PointF(3.5f, 9.2f),
        };

for (String inputFile : fileNames) {
    for (com.aspose.imaging.PointF scale : scales) {
        String outputFile = String.format("%s_%2.2f_%2.2f.png", inputFile, scale.getX(), scale.getY());
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + inputFile);
        try {
            image.resize((int) (image.getWidth() * scale.getX()), (int) (image.getHeight() * scale.getY()));
            image.save(dir + outputFile, new com.aspose.imaging.imageoptions.PngOptions());
        }
        finally {
            image.close();
        }
    }
}

resize(int newWidth, int newHeight, int resizeType)

public void resize(int newWidth, int newHeight, int resizeType)

调整图像大小。

Parameters:

参数类型描述
newWidthint新的宽度。
newHeightint新的高度。
resizeTypeint调整大小类型。

Example: This example loads an image and resizes it using various resizing methods.

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用最近邻重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "upsample.nearestneighbour.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用最近邻重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "downsample.nearestneighbour.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用双线性重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "upsample.bilinear.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用双线性重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "downsample.bilinear.gif");
} finally {
    image.dispose();
}

Example: This example loads a raster image and resizes it using various resizing methods.

String dir = "c:\\temp\\";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif")) {
    // 使用最近邻重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "upsample.nearestneighbour.gif");
}
            
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif")) {
    // 使用最近邻重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "downsample.nearestneighbour.gif");
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif")) {
    // 使用双线性重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "upsample.bilinear.gif");
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif")) {
    // 使用双线性重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "downsample.bilinear.gif");
}

Example: This example loads a WMF image and resizes it using various resizing methods.

String dir = "c:\\temp\\";
            
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.wmf")) {
    // 使用最近邻重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.wmf")) {
    // 使用最近邻重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.wmf")) {
    // 使用双线性重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.wmf")) {
    // 使用双线性重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);
}

Example: This example loads a multi-page ODG image and resizes it using various resizing methods.

String dir = "c:\\temp\\";
            
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.odg")) {
    // 使用最近邻重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);

    // 使用默认选项保存为 PNG。
    image.save(dir + "upsample.nearestneighbour.png", new com.aspose.imaging.imageoptions.PngOptions());
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.odg")) {
    // 使用最近邻重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);

    // 使用默认选项保存为 PNG。
    image.save(dir + "downsample.nearestneighbour.png", new com.aspose.imaging.imageoptions.PngOptions());
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.odg")) {
    // 使用双线性重采样将尺寸放大 2 倍。
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);

    // 使用默认选项保存为 PNG。
    image.save(dir + "upsample.bilinear.png", new com.aspose.imaging.imageoptions.PngOptions());
}

try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.odg")) {
    // 使用双线性重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);

    // 使用默认选项保存为 PNG。
    image.save(dir + "downsample.bilinear.png", new com.aspose.imaging.imageoptions.PngOptions());
}

Example: Using a segment mask to speed up the segmentation process

// 遮罩导出选项
com.aspose.imaging.imageoptions.PngOptions exportOptions = new com.aspose.imaging.imageoptions.PngOptions();
exportOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha);
exportOptions.setSource(new com.aspose.imaging.sources.StreamSource());

com.aspose.imaging.masking.options.MaskingOptions maskingOptions = new com.aspose.imaging.masking.options.MaskingOptions();

// 使用 GraphCut 聚类。
maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.GraphCut);
maskingOptions.setDecompose(false);
maskingOptions.setArgs(new com.aspose.imaging.masking.options.AutoMaskingArgs());

// 背景颜色将是透明的。
maskingOptions.setBackgroundReplacementColor(com.aspose.imaging.Color.getTransparent());
maskingOptions.setExportOptions(exportOptions);

String dir = "c:\\temp\\";
com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "BigImage.jpg");
try
{
    com.aspose.imaging.Size imageSize = image.getSize();

    // 减小图像尺寸以加快分割过程
    image.resizeHeightProportionally(600, com.aspose.imaging.ResizeType.HighQualityResample);

    // 创建 ImageMasking 类的实例。
    com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image);

    // 将源图像划分为多个簇(段)。
    com.aspose.imaging.masking.result.MaskingResult maskingResult = masking.decompose(maskingOptions);
    try
    {
        // 获取前景掩码
        com.aspose.imaging.RasterImage foregroundMask = maskingResult.get_Item(1).getMask();
        try
        {
            // 将掩码的大小增加到原始图像的尺寸
            foregroundMask.resize(imageSize.getWidth(), imageSize.getHeight(), com.aspose.imaging.ResizeType.NearestNeighbourResample);

            // 将掩码应用于原始图像以获得前景段
            com.aspose.imaging.RasterImage originImage = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "BigImage.jpg");
            try
            {
                com.aspose.imaging.masking.ImageMasking.applyMask(originImage, foregroundMask, maskingOptions);
                originImage.save(dir + "BigImage_foreground.png", exportOptions);
            }
            finally
            {
                originImage.close();
            }
        }
        finally
        {
            foregroundMask.close();
        }
    }
    finally
    {
        maskingResult.close();
    }
}
finally
{
    image.close();
}

Example: Resize image using specific Resize Type.

try (Image image = Image.load("Photo.jpg"))
{
    image.resize(640, 480, ResizeType.CatmullRom);
    image.save("ResizedPhoto.jpg");

    image.resize(1024, 768, ResizeType.CubicConvolution);
    image.save("ResizedPhoto2.jpg");

    ImageResizeSettings resizeSettings = new ImageResizeSettings();
    resizeSettings.setMode(ResizeType.CubicBSpline);
    resizeSettings.setFilterType(ImageFilterType.SmallRectangular);

    image.resize(800, 800, resizeSettings);
    image.save("ResizedPhoto3.jpg");
}

Example: Resize EPS image and export it to PNG format.

// 加载 EPS 图像
try (Image image = Image.load("AstrixObelix.eps"))
{
    // 使用 Mitchell 三次插值方法调整图像大小
    image.resize(400, 400, ResizeType.Mitchell);

    // 将图像导出为 PNG 格式
    image.save("ExportResult.png", new PngOptions());
}

resize(int newWidth, int newHeight, ImageResizeSettings settings)

public abstract void resize(int newWidth, int newHeight, ImageResizeSettings settings)

调整图像大小。

Parameters:

参数类型描述
newWidthint新的宽度。
newHeightint新的高度。
settingsImageResizeSettings调整大小的设置。

Example: This example loads an image and resizes it using various resizing settings.

String dir = "c:\\temp\\";

com.aspose.imaging.ImageResizeSettings resizeSettings = new com.aspose.imaging.ImageResizeSettings();

// 基于加权和混合有理函数以及 lanczos3 插值的自适应算法。
resizeSettings.setMode(com.aspose.imaging.ResizeType.AdaptiveResample);

// 小矩形滤波器
resizeSettings.setFilterType(com.aspose.imaging.ImageFilterType.SmallRectangular);

// 调色板中的颜色数量。
resizeSettings.setEntriesCount(256);

// 未使用颜色量化
resizeSettings.setColorQuantizationMethod(com.aspose.imaging.ColorQuantizationMethod.None);

// 欧几里得方法
resizeSettings.setColorCompareMethod(com.aspose.imaging.ColorCompareMethod.Euclidian);

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用自适应重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, resizeSettings);
    image.save(dir + "downsample.adaptive.gif");
} finally {
    image.dispose();
}

Example: This example loads a raster image and resizes it using various resizing settings.

String dir = "c:\\temp\\";

com.aspose.imaging.ImageResizeSettings resizeSettings = new com.aspose.imaging.ImageResizeSettings();

// 基于加权和混合有理函数以及 lanczos3 插值的自适应算法。
resizeSettings.setMode(com.aspose.imaging.ResizeType.AdaptiveResample);

// 小矩形滤波器
resizeSettings.setFilterType(com.aspose.imaging.ImageFilterType.SmallRectangular);

// 调色板中的颜色数量。
resizeSettings.setEntriesCount(256);

// 未使用颜色量化
resizeSettings.setColorQuantizationMethod(com.aspose.imaging.ColorQuantizationMethod.None);

// 欧几里得方法
resizeSettings.setColorCompareMethod(com.aspose.imaging.ColorCompareMethod.Euclidian);
            
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif")) {
    // 使用自适应重采样将尺寸缩小 2 倍。
    image.resize(image.getWidth() / 2, image.getHeight() / 2, resizeSettings);
    image.save(dir + "downsample.adaptive.gif");
}

Example: Resize image using specific Resize Type.

try (Image image = Image.load("Photo.jpg"))
{
    image.resize(640, 480, ResizeType.CatmullRom);
    image.save("ResizedPhoto.jpg");

    image.resize(1024, 768, ResizeType.CubicConvolution);
    image.save("ResizedPhoto2.jpg");

    ImageResizeSettings resizeSettings = new ImageResizeSettings();
    resizeSettings.setMode(ResizeType.CubicBSpline);
    resizeSettings.setFilterType(ImageFilterType.SmallRectangular);

    image.resize(800, 800, resizeSettings);
    image.save("ResizedPhoto3.jpg");
}

Example: Resize EPS image using advanced settings.

// 加载 EPS 图像
try (Image image = Image.load("AstrixObelix.eps"))
{
    ImageResizeSettings resizeSettings = new ImageResizeSettings();
    // 设置插值模式
    resizeSettings.setMode(ResizeType.LanczosResample);
    // 设置过滤器的类型
    resizeSettings.setFilterType(ImageFilterType.SmallRectangular);
    // 设置颜色比较方法
    resizeSettings.setColorCompareMethod(ColorCompareMethod.Euclidian);
    // 设置颜色量化方法
    resizeSettings.setColorQuantizationMethod(ColorQuantizationMethod.Popularity);

    // 使用高级调整设置调整图像大小
    image.resize(400, 400, resizeSettings);

    // 将图像导出为 PNG 格式
    image.save("ExportResult.png", new PngOptions());
}

getDefaultOptions(Object[] args)

public ImageOptionsBase getDefaultOptions(Object[] args)

获取默认选项。

Parameters:

参数类型描述
argsjava.lang.Object[]参数。

Returns: ImageOptionsBase - Default options

getOriginalOptions()

public ImageOptionsBase getOriginalOptions()

根据原始文件设置获取选项。这有助于保持原始图像的位深度和其他参数不变。例如,如果我们加载一张每像素 1 位的黑白 PNG 图像,然后使用 DataStreamSupporter.Save(string) 方法保存,它将生成每像素 8 位的输出 PNG 图像。为避免这种情况并以每像素 1 位保存 PNG 图像,请使用此方法获取相应的保存选项,并将其作为第二个参数传递给 Image.Save(string, ImageOptionsBase) 方法。

Returns: ImageOptionsBase - The options based on the original file settings.

resizeWidthProportionally(int newWidth)

public void resizeWidthProportionally(int newWidth)

按比例调整宽度。默认使用 ResizeType.NearestNeighbourResample

Parameters:

参数类型描述
newWidthint新的宽度。

resizeHeightProportionally(int newHeight)

public void resizeHeightProportionally(int newHeight)

按比例调整高度。默认使用 ResizeType.NearestNeighbourResample

Parameters:

参数类型描述
newHeightint新的高度。

resizeWidthProportionally(int newWidth, int resizeType)

public void resizeWidthProportionally(int newWidth, int resizeType)

按比例调整宽度。

Parameters:

参数类型描述
newWidthint新的宽度。
resizeTypeint调整的类型。

Example: This example loads an image and resizes it proportionally using various resizing methods. 此示例加载图像并使用各种缩放方法按比例调整大小。仅指定宽度,高度会自动计算。

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用最近邻重采样将尺寸放大 2 倍。
    image.resizeWidthProportionally(image.getWidth() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "upsample.nearestneighbour.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用最近邻重采样将尺寸缩小 2 倍。
    image.resizeWidthProportionally(image.getWidth() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "downsample.nearestneighbour.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用双线性重采样将尺寸放大 2 倍。
    image.resizeWidthProportionally(image.getWidth() * 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "upsample.bilinear.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
{
    // 使用双线性重采样将尺寸缩小 2 倍。
    image.resizeWidthProportionally(image.getWidth() / 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "downsample.bilinear.gif");
}

resizeHeightProportionally(int newHeight, int resizeType)

public void resizeHeightProportionally(int newHeight, int resizeType)

按比例调整高度。

Parameters:

参数类型描述
newHeightint新的高度。
resizeTypeint调整的类型。

Example: This example loads an image and resizes it proportionally using various resizing methods. 此示例加载图像并使用各种缩放方法按比例调整大小。仅指定高度,宽度会自动计算。

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用最近邻重采样将尺寸放大 2 倍。
    image.resizeHeightProportionally(image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "upsample.nearestneighbour.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用最近邻重采样将尺寸缩小 2 倍。
    image.resizeHeightProportionally(image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
    image.save(dir + "upsample.nearestneighbour.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用双线性重采样将尺寸放大 2 倍。
    image.resizeHeightProportionally(image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "upsample.bilinear.gif");
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.gif");
try {
    // 使用双线性重采样将尺寸缩小 2 倍。
    image.resizeHeightProportionally(image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);
    image.save(dir + "downsample.bilinear.gif");
} finally {
    image.dispose();
}

Example: Using a segment mask to speed up the segmentation process

// 遮罩导出选项
com.aspose.imaging.imageoptions.PngOptions exportOptions = new com.aspose.imaging.imageoptions.PngOptions();
exportOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha);
exportOptions.setSource(new com.aspose.imaging.sources.StreamSource());

com.aspose.imaging.masking.options.MaskingOptions maskingOptions = new com.aspose.imaging.masking.options.MaskingOptions();

// 使用 GraphCut 聚类。
maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.GraphCut);
maskingOptions.setDecompose(false);
maskingOptions.setArgs(new com.aspose.imaging.masking.options.AutoMaskingArgs());

// 背景颜色将是透明的。
maskingOptions.setBackgroundReplacementColor(com.aspose.imaging.Color.getTransparent());
maskingOptions.setExportOptions(exportOptions);

String dir = "c:\\temp\\";
com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "BigImage.jpg");
try
{
    com.aspose.imaging.Size imageSize = image.getSize();

    // 减小图像尺寸以加快分割过程
    image.resizeHeightProportionally(600, com.aspose.imaging.ResizeType.HighQualityResample);

    // 创建 ImageMasking 类的实例。
    com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image);

    // 将源图像划分为多个簇(段)。
    com.aspose.imaging.masking.result.MaskingResult maskingResult = masking.decompose(maskingOptions);
    try
    {
        // 获取前景掩码
        com.aspose.imaging.RasterImage foregroundMask = maskingResult.get_Item(1).getMask();
        try
        {
            // 将掩码的大小增加到原始图像的尺寸
            foregroundMask.resize(imageSize.getWidth(), imageSize.getHeight(), com.aspose.imaging.ResizeType.NearestNeighbourResample);

            // 将掩码应用于原始图像以获得前景段
            com.aspose.imaging.RasterImage originImage = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "BigImage.jpg");
            try
            {
                com.aspose.imaging.masking.ImageMasking.applyMask(originImage, foregroundMask, maskingOptions);
                originImage.save(dir + "BigImage_foreground.png", exportOptions);
            }
            finally
            {
                originImage.close();
            }
        }
        finally
        {
            foregroundMask.close();
        }
    }
    finally
    {
        maskingResult.close();
    }
}
finally
{
    image.close();
}

resizeWidthProportionally(int newWidth, ImageResizeSettings settings)

public void resizeWidthProportionally(int newWidth, ImageResizeSettings settings)

按比例调整宽度。

Parameters:

参数类型描述
newWidthint新的宽度。
settingsImageResizeSettings图像调整设置。

resizeHeightProportionally(int newHeight, ImageResizeSettings settings)

public void resizeHeightProportionally(int newHeight, ImageResizeSettings settings)

按比例调整高度。

Parameters:

参数类型描述
newHeightint新的高度。
settingsImageResizeSettings图像调整设置。

rotateFlip(int rotateFlipType)

public abstract void rotateFlip(int rotateFlipType)

旋转、翻转,或同时旋转和翻转图像。

Parameters:

参数类型描述
rotateFlipTypeint旋转翻转的类型。

Example: This example demonstrates the use of Rotate operation on an image. 此示例演示了在图像上使用 Rotate 操作。示例从某个磁盘位置加载现有图像文件,并根据枚举 com.aspose.imaging.RotateFlipType 的值对图像执行 Rotate 操作。

// 创建 image 类的实例,并通过文件路径使用现有图像文件进行初始化。
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp");
try {
    // 围绕 X 轴将图像旋转 180 度。
    image.rotateFlip(com.aspose.imaging.RotateFlipType.Rotate180FlipX);

    // 保存所有更改。
    image.save();
} finally {
    image.dispose();
}

rotate(float angle)

public void rotate(float angle)

围绕中心旋转图像。

Parameters:

参数类型描述
anglefloat旋转角度(以度为单位)。正值将顺时针旋转。

crop(Rectangle rectangle)

public void crop(Rectangle rectangle)

裁剪指定的矩形。

Parameters:

参数类型描述
rectangleRectangle矩形。

Example: The following example crops a raster image. 以下示例裁剪光栅图像。裁剪区域通过 com.aspose.imaging.Rectangle 指定。

String dir = "c:\\temp\\";
            
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png")) {
    // 裁剪图像。裁剪区域是图像的中心矩形区域。
    com.aspose.imaging.Rectangle area = new com.aspose.imaging.Rectangle(rasterImage.getWidth() / 4, rasterImage.getHeight() / 4, rasterImage.getWidth() / 2, rasterImage.getHeight() / 2);
    image.crop(area);

    // 将裁剪后的图像保存为 PNG
    image.save(dir + "sample.Crop.png");
}

crop(int leftShift, int rightShift, int topShift, int bottomShift)

public void crop(int leftShift, int rightShift, int topShift, int bottomShift)

使用位移裁剪图像。

Parameters:

参数类型描述
leftShiftint左位移。
rightShiftint右位移。
topShiftint上位移。
bottomShiftint下位移。

Example: The following example crops a raster image. 以下示例裁剪光栅图像。裁剪区域通过左、上、右、下边距指定。

String dir = "c:\\temp\\";
            
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png")) {
    // 再次裁剪。设置图像尺寸的 10% 边距。
    int horizontalMargin = rasterImage.getWidth() / 10;
    int verticalMargin = rasterImage.getHeight() / 10;
    image.crop(horizontalMargin, horizontalMargin, verticalMargin, verticalMargin);

    // 将裁剪后的图像保存为 PNG。
    image.save(dir + "sample.Crop.png");
}

save()

public final void save()

将图像数据保存到底层流。

Example: The following example shows how to save an entire BMP image or part of it to a file or stream.

String dir = "c:\\temp\\";
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.bmp");
try {
    com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = (com.aspose.imaging.fileformats.bmp.BmpImage) image;

    // 转换为黑白图像
    bmpImage.binarizeOtsu();

    // 使用默认选项保存到相同位置。
    image.save();

    com.aspose.imaging.imageoptions.BmpOptions saveOptions = new com.aspose.imaging.imageoptions.BmpOptions();

    // 在此情况下,调色板仅包含两种颜色:黑色和白色。
    saveOptions.setPalette(com.aspose.imaging.ColorPaletteHelper.createMonochrome());

    // 对于所有单色图像(包括黑白图像),分配每像素 1 位即可。
    saveOptions.setBitsPerPixel(1);

    // 使用指定选项保存到其他位置。
    image.save(dir + "sample.bw.palettized.bmp", saveOptions);

    // 仅保存图像的中心部分。
    com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(image.getWidth() / 4, image.getHeight() / 4, image.getWidth() / 2, image.getHeight() / 2);
    image.save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds);

    // 将整个图像保存到内存流
    java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
    try {
        image.save(stream, saveOptions);
        System.out.println("The size of the whole image in bytes: " + stream.size());
    } finally {
        stream.close();
    }

    // 将图像的中心部分保存到内存流
    stream = new java.io.ByteArrayOutputStream();
    try {
        image.save(stream, saveOptions, bounds);
        System.out.println("The size of the central part of the image in bytes: " + stream.size());
    } finally {
        stream.close();
    }
} finally {
    image.close();
}

//输出可能如下所示:
//整个图像的大小(字节):1662
//图像中心部分的大小(字节):462

save(String filePath)

public void save(String filePath)

将图像保存到指定的文件位置。

Parameters:

参数类型描述
filePathjava.lang.String保存图像的文件路径。

save(String filePath, ImageOptionsBase options)

public void save(String filePath, ImageOptionsBase options)

根据保存选项,将对象的数据以指定的文件格式保存到指定的文件位置。

Parameters:

参数类型描述
filePathjava.lang.String文件路径。
optionsImageOptionsBase选项。

Example: This example shows the simple steps to Save an Image. 此示例展示了保存图像的简单步骤。为演示此操作,我们从某个磁盘位置加载现有文件,并以 PSD 格式保存图像。

// 从磁盘加载现有文件。
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp");
try {
    // 使用默认的 PsdOptions 设置将图像保存为 PSD 到文件路径。
    image.save("C:\\temp\\output.psd", new com.aspose.imaging.imageoptions.PsdOptions());
} finally {
    image.dispose();
}

save(String filePath, ImageOptionsBase options, Rectangle boundsRectangle)

public void save(String filePath, ImageOptionsBase options, Rectangle boundsRectangle)

根据保存选项,将对象的数据以指定的文件格式保存到指定的文件位置。

Parameters:

参数类型描述
filePathjava.lang.String文件路径。
optionsImageOptionsBase选项。
boundsRectangleRectangle目标图像边界矩形。设置空矩形以使用源边界。

Example: The following example loads a BMP image from a file, then saves a rectangular part of the image to a PNG file.

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.bmp");
try {
    // 将图像的上半部分保存为 PNG 文件。
    com.aspose.imaging.imageoptions.PngOptions saveOptions = new com.aspose.imaging.imageoptions.PngOptions();
    com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(0, 0, image.getWidth(), image.getHeight() / 2);
    image.save(dir + "output.png", saveOptions, bounds);
} finally {
    image.dispose();
}

save(RandomAccessFile file, ImageOptionsBase options)

public void save(RandomAccessFile file, ImageOptionsBase options)

根据保存选项,将对象的数据以指定的文件格式保存到指定的文件位置。

Parameters:

参数类型描述
文件java.io.RandomAccessFile用于保存图像数据的文件。
optionsImageOptionsBase选项。

save(RandomAccessFile file, ImageOptionsBase optionsBase, Rectangle boundsRectangle)

public void save(RandomAccessFile file, ImageOptionsBase optionsBase, Rectangle boundsRectangle)

根据保存选项,将图像的数据以指定的文件格式保存到指定的流中。

Parameters:

参数类型描述
文件java.io.RandomAccessFile用于保存图像数据的文件。
optionsBaseImageOptionsBase保存选项。
boundsRectangleRectangle目标图像边界矩形。设置空矩形以使用源边界。

save(OutputStream stream, ImageOptionsBase optionsBase)

public void save(OutputStream stream, ImageOptionsBase optionsBase)

根据保存选项,将图像的数据以指定的文件格式保存到指定的流中。

Parameters:

参数类型描述
java.io.OutputStream用于保存图像数据的流。
optionsBaseImageOptionsBase保存选项。

Example: This example shows the process of saving an Image to memory buffer. 此示例展示了将 Image 保存到内存缓冲区的过程。为了演示此操作,示例从某个磁盘位置加载现有文件并以 PSD 格式保存图像。

java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
try {            //Create an instance of image class and initialize it with an existing image file through File path
    com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp");
    try {
        //使用默认的 PsdOptions 设置将图像保存到 PSD 内存流
        image.save(stream, new com.aspose.imaging.imageoptions.PsdOptions());
    } finally {
        image.dispose();
    }
} finally {
    stream.close();
}

save(OutputStream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)

public void save(OutputStream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)

根据保存选项,将图像的数据以指定的文件格式保存到指定的流中。

Parameters:

参数类型描述
java.io.OutputStream用于保存图像数据的流。
optionsBaseImageOptionsBase保存选项。
boundsRectangleRectangle目标图像边界矩形。设置空矩形以使用源边界。

Example: The following example loads an image from a file, then saves a rectangular part of the image to a PNG file stream.

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.bmp");
try {
    com.aspose.imaging.imageoptions.PngOptions saveOptions = new com.aspose.imaging.imageoptions.PngOptions();
    com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(0, 0, image.getWidth(), image.getHeight() / 2);
    java.io.OutputStream outputStream = new java.io.FileOutputStream(dir + "sample.output.png");
    try {
        // 将图像的上半部分保存到文件流。
        image.save(outputStream, saveOptions, bounds);
    } finally {
        outputStream.close();
    }
} finally {
    image.dispose();
}

setPalette(IColorPalette palette, boolean updateColors)

public abstract void setPalette(IColorPalette palette, boolean updateColors)

设置图像调色板。

Parameters:

参数类型描述
paletteIColorPalette要设置的调色板。
updateColorsboolean如果设置为 true,颜色将根据新调色板进行更新;否则颜色索引保持不变。请注意,如果某些索引没有对应的调色板条目,未更改的索引可能在加载时导致图像崩溃。

getSerializedStream(ImageOptionsBase imageOptions, Rectangle clippingRectangle, int[] pageNumber)

public InputStream getSerializedStream(ImageOptionsBase imageOptions, Rectangle clippingRectangle, int[] pageNumber)

转换为 aps。

Parameters:

参数类型描述
imageOptionsImageOptionsBase图像选项。
clippingRectangleRectangle裁剪矩形。
pageNumberint[]页码。

Returns: java.io.InputStream - 序列化流