【问题标题】:Combined re-scaling and color reduction of an image in Java?Java中图像的组合重新缩放和颜色减少?
【发布时间】:2009-08-25 21:00:20
【问题描述】:
给定一个矩形输入图像,我想使用最多 10 种颜色创建一个大小为 40x40 像素的输出图像。因此,需要的两个操作是重新缩放和颜色减少。
以下 ImageMagick 命令可以解决问题:
convert input.png -scale 40x40 -colors 10 output.png
你将如何在 Java 中实现相应的结果?
选择 ImageMagick 不是一种选择 :-)
【问题讨论】:
标签:
java
graphics
image-manipulation
imagemagick
java-2d
【解决方案1】:
这样的事情可以使用 JAI:
// now resize the image
ParameterBlock pb = new ParameterBlock();
pb.addSource(image); // The source image
pb.add(wScale); // The xScale
pb.add(hScale); // The yScale
pb.add(0.0F); // The x translation
pb.add(0.0F); // The y translation
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
RenderedOp resizedImage = JAI.create("SubsampleAverage", pb, hints);
// lastly, write the newly-resized image to an
// output stream, in a specific encoding
try
{
FileOutputStream fos = new FileOutputStream(new File(filename));
JAI.create("encode", resizedImage, fos, getImageType(filename), null);
ParameterBlock ParameterBlock pb = new ParameterBlock();
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.TYPE_YCbCr), new int[] {8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
pb.add(cm);
RenderedOp imgycc = JAI.create("ColorConvert", pb);
}
catch (FileNotFoundException e)
{
}
【解决方案2】:
在 jai.dev.java.net 上查找 Java 高级成像 (JAI)