ColorPaletteHelper
Inheritance: java.lang.Object
public final class ColorPaletteHelper
用于颜色调色板操作的帮助类。
方法
createMonochrome()
public static IColorPalette createMonochrome()
创建仅包含 2 种颜色的单色调色板。
Returns: IColorPalette - Color palette for monochrome images.
create4Bit()
public static IColorPalette create4Bit()
创建 4 位颜色调色板。
Returns: IColorPalette - The 4 bit color palette.
create4BitGrayscale(boolean minIsWhite)
public static IColorPalette create4BitGrayscale(boolean minIsWhite)
创建 4 位灰度调色板。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| minIsWhite | boolean | 如果设置为 true,调色板以白色开始,否则以黑色开始。 |
Returns: IColorPalette - The 4 bit grayscale palette.
create8Bit()
public static IColorPalette create8Bit()
创建 8 位颜色调色板。
Returns: IColorPalette - The 8bit color palette.
create8BitGrayscale(boolean minIsWhite)
public static IColorPalette create8BitGrayscale(boolean minIsWhite)
创建 8 位灰度调色板。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| minIsWhite | boolean | 如果设置为 true,调色板以白色开始,否则以黑色开始。 |
Returns: IColorPalette - The 8 bit grayscale palette.
Example: The following example creates a palettized grayscale BMP image and then saves it to a file.
String dir = "c:\\temp\\";
com.aspose.imaging.imageoptions.BmpOptions createOptions = new com.aspose.imaging.imageoptions.BmpOptions();
// 保存到文件
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource(dir + "output.palette8bit.bmp", false));
// 使用每像素 8 位以减小输出图像的大小。
createOptions.setBitsPerPixel(8);
// 设置覆盖所有灰度颜色的标准 8 位灰度调色板。
// 如果处理后的图像仅包含灰度颜色,则其调色版本
// 在视觉上与未调色的图像无差别。
createOptions.setPalette(com.aspose.imaging.ColorPaletteHelper.create8BitGrayscale(false));
// 不使用压缩保存。
// 您也可以使用 RLE-8 压缩来减小输出图像的大小。
createOptions.setCompression(com.aspose.imaging.fileformats.bmp.BitmapCompression.Rgb);
// 将水平和垂直分辨率设置为 96 dpi。
createOptions.setResolutionSettings(new com.aspose.imaging.ResolutionSetting(96.0, 96.0));
// 创建 100 x 100 像素的 BMP 图像并保存到文件。
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(image);
com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
new com.aspose.imaging.Point(0, 0),
new com.aspose.imaging.Point(image.getWidth(), image.getHeight()),
com.aspose.imaging.Color.getBlack(),
com.aspose.imaging.Color.getWhite());
// 用灰度渐变填充图像
graphics.fillRectangle(gradientBrush, image.getBounds());
image.save();
} finally {
image.dispose();
}
getCloseImagePalette(RasterImage image, int entriesCount)
public static IColorPalette getCloseImagePalette(RasterImage image, int entriesCount)
在图像没有调色板的情况下,从光栅图像获取调色板(对图像进行调色)。如果调色板已存在,则直接使用而不进行计算。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| entriesCount | int | 所需的条目计数。 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
Example: The following example shows how to palletize a BMP image to reduce its output size.
// 创建一个 100 x 100 像素的 BMP 图像。
com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = new com.aspose.imaging.fileformats.bmp.BmpImage(100, 100);
try {
// 图像左上角到右下角的线性渐变。
com.aspose.imaging.brushes.LinearGradientBrush brush =
new com.aspose.imaging.brushes.LinearGradientBrush(
new com.aspose.imaging.Point(0, 0),
new com.aspose.imaging.Point(bmpImage.getWidth(), bmpImage.getHeight()),
com.aspose.imaging.Color.getRed(),
com.aspose.imaging.Color.getGreen());
// 使用线性渐变画刷填充整个图像。
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(bmpImage);
gr.fillRectangle(brush, bmpImage.getBounds());
// 获取最接近的 8 位颜色调色板,覆盖尽可能多的像素,以便调色板图像
// 几乎在视觉上与非调色板图像无法区分。
com.aspose.imaging.IColorPalette palette = com.aspose.imaging.ColorPaletteHelper.getCloseImagePalette(bmpImage, 256);
// 8 位调色板最多包含 256 种颜色。
com.aspose.imaging.imageoptions.BmpOptions saveOptions = new com.aspose.imaging.imageoptions.BmpOptions();
saveOptions.setPalette(palette);
saveOptions.setBitsPerPixel(8);
java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
try {
bmpImage.save(stream, saveOptions);
System.out.println("The palettized image size is " + stream.size() + " bytes.");
} finally {
stream.close();
}
stream = new java.io.ByteArrayOutputStream();
try {
bmpImage.save(stream);
System.out.println("The non-palettized image size is " + stream.size() + " bytes.");
} finally {
stream.close();
}
} finally {
bmpImage.dispose();
}
// 输出如下:
// 调色板化的图像大小为 11078 字节。
// 非调色板化的图像大小为 40054 字节。
getCloseTransparentImagePalette(RasterImage image, int entriesCount)
public static IColorPalette getCloseTransparentImagePalette(RasterImage image, int entriesCount)
在图像没有调色板的情况下,从光栅图像获取调色板(对图像进行调色)。如果调色板已存在,则直接使用而不进行计算。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| entriesCount | int | 所需的条目计数。 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
getCloseImagePalette(RasterImage image, int entriesCount, int paletteMiningMethod)
public static IColorPalette getCloseImagePalette(RasterImage image, int entriesCount, int paletteMiningMethod)
如果图像没有调色板,则从光栅图像获取颜色调色板(对图像进行调色)。调色板将被优化以获得更好的索引图像质量,或者在使用 PaletteMiningMethod.UseCurrentPalette 时保持"AS IS"。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| entriesCount | int | 所需的条目计数。 |
| paletteMiningMethod | int | 调色板挖掘方法。 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
Example: The following example shows how to compress a PNG image, using indexed color with best fit palette
// 加载 png 图像
String sourceFilePath = "OriginalRings.png";
String outputFilePath = "OriginalRingsOutput.png";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(sourceFilePath))
{
com.aspose.imaging.imageoptions.PngOptions options = new com.aspose.imaging.imageoptions.PngOptions();
options.setProgressive(true);
// 使用索引颜色类型
options.setColorType(com.aspose.imaging.fileformats.png.PngColorType.IndexedColor);
// 使用最大压缩
options.setCompressionLevel(9);
// 获取最接近的 8 位颜色调色板,覆盖尽可能多的像素,以便调色板图像
// 几乎在视觉上与非调色板图像无法区分。
options.setPalette(com.aspose.imaging.ColorPaletteHelper.getCloseImagePalette((com.aspose.imaging.RasterImage)image,
256, Aspose.Imaging.PaletteMiningMethod.Histogram));
image.save(outputFilePath, options);
}
// 输出文件大小应显著减小
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)
public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)
在图像没有调色板的情况下,从光栅图像获取调色板(对图像进行调色)。如果调色板已存在,则直接使用而不进行计算。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| destBounds | Rectangle | 目标图像的边界。 |
| entriesCount | int | 所需的条目计数。 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette)
public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette)
在图像没有调色板的情况下,从光栅图像获取调色板(对图像进行调色)。如果调色板已存在,则直接使用而不进行计算。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| destBounds | Rectangle | 目标图像的边界。 |
| entriesCount | int | 所需的条目计数。 |
| useImagePalette | boolean | 如果设置,它将在可用时使用其自己的图像调色板 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor)
public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor)
在图像没有调色板的情况下,从光栅图像获取调色板(对图像进行调色)。如果调色板已存在,则直接使用而不进行计算。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| destBounds | Rectangle | 目标图像的边界。 |
| entriesCount | int | 所需的条目计数。 |
| useImagePalette | boolean | 如果设置,它将在可用时使用其自己的图像调色板 |
| alphaBlendInColor | Color | 应作为半透明 alpha 替换的背景颜色的颜色。 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor, boolean keepTransparency)
public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor, boolean keepTransparency)
在图像没有调色板的情况下,从光栅图像获取调色板(对图像进行调色)。如果调色板已存在,则直接使用而不进行计算。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 光栅图像。 |
| destBounds | Rectangle | 目标图像的边界。 |
| entriesCount | int | 所需的条目计数。 |
| useImagePalette | boolean | 如果设置,它将在可用时使用其自己的图像调色板 |
| alphaBlendInColor | Color | 应作为半透明 alpha 替换的背景颜色的颜色。 |
| keepTransparency | boolean | 如果设置,它将考虑图像颜色的 alpha 通道位。 |
Returns:
IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
getUniformColorPalette(RasterImage image)
public static ColorPalette getUniformColorPalette(RasterImage image)
获取统一的 256 色调色板。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 图像。 |
Returns:
ColorPalette - The ColorPalette.
getDownscalePalette(RasterImage image)
public static ColorPalette getDownscalePalette(RasterImage image)
获取 256 色调色板,由初始图像颜色值的高位组成。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 图像。 |
Returns:
ColorPalette - The ColorPalette.
hasTransparentColors(IColorPalette palette)
public static boolean hasTransparentColors(IColorPalette palette)
确定指定的调色板是否包含透明颜色。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| palette | IColorPalette | 调色板。 |
Returns:
布尔值 - 如果指定的调色板具有透明颜色,则为 true;否则为 false。
createGrayscale(int bits)
public static IColorPalette createGrayscale(int bits)
获取指定位数的灰度调色板。允许的位值为 1、2、4、8。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| bits | int | 位计数。 |
Returns: IColorPalette - Grayscale palette.