gpt4 book ai didi

java - 在 Java 中调整索引图像的大小而不丢失透明度

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:24 25 4
gpt4 key购买 nike

这是我调整图像大小的功能。质量不是 Photoshop,但可以接受。

Not Acceptable 是索引 png 上的行为。
我们希望,如果我们使用具有透明索引的 256 色调色板缩小图像,我们将获得具有相同透明度的调整大小的图像,但事实并非如此。

因此我们调整了新的 ARGB 图像的大小,然后将其减少为 256 色。问题是如何“重新引入”透明像素索引。

private static BufferedImage internalResize(BufferedImage source, int destWidth, int destHeight) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
double xScale = ((double) destWidth) / (double) sourceWidth;
double yScale = ((double) destHeight) / (double) sourceHeight;
Graphics2D g2d = null;

BufferedImage resizedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TRANSLUCENT);

log.debug("resizing image to w:" + destWidth + " h:" + destHeight);
try {

g2d = resizedImage.createGraphics();

g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);

g2d.drawRenderedImage(source, at);

} finally {
if (g2d != null)
g2d.dispose();
}

//doesn't keep the transparency
if (source.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
log.debug("reducing to color-indexed image");

BufferedImage indexedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_BYTE_INDEXED);

try {
Graphics g = indexedImage.createGraphics();
g.drawImage(resizedImage, 0, 0, null);
} finally {
if (g != null)
g.dispose();
}
System.err.println("source" + ((IndexColorModel) source.getColorModel()).getTransparentPixel()
+ " " + ((IndexColorModel) indexedImage.getColorModel()).getTransparentPixel());

return indexedImage;
}

return resizedImage;

}

最佳答案

尝试改变

BufferedImage indexedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_BYTE_INDEXED);

    BufferedImage indexedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_BYTE_INDEXED, (IndexColorModel) source.getColorModel());

即使这对您没有特别帮助(如果调整大小,无论出于何种原因,它可能不会改变索引的特定颜色值),您可以创建一个新的 BufferedImage使用给定的 IndexColorModel 可能对您非常有用。

http://download.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html#BufferedImage%28int,%20int,%20int,%20java.awt.image.IndexColorModel%29

编辑:刚刚注意到您的 resizedImage 构造函数可能应该使用 BufferedImage.TYPE_INT_ARGB 而不是 BufferedImage.TRANSLUCENT。不确定这是否会改变它的工作方式,但不应将 BufferedImage.TRANSLUCENT 传递给构造函数的那种形式。 http://download.oracle.com/javase/1,5.0/docs/api/java/awt/image/BufferedImage.html#BufferedImage%28int,%20int,%20int%29

无论如何,也许可以尝试这样的事情:

DirectColorModel resizedModel = (DirectColorModel) resizedImage.getColorModel();
int numPixels = resizedImage.getWidth() * resizedImage.getHeight();

byte[numPixels] reds;
byte[numPixels] blues;
byte[numPixels] greens;
byte[numPixels] alphas;
int curIndex = 0;
int curPixel;

for (int i = 0; i < resizedImage.getWidth(); i++)
{
for (int j = 0; j < resizedImage.getHeight(); j++)
{
curPixel = resizedImage.getRGB(i, j);
reds[curIndex] = resizedModel.getRed(curPixel);
blues[curIndex]= resizedModel.getBlue(curPixel);
greens[curIndex] = resizedModel.getGreen(curPixel);
alphas[curIndex] = resizedModel.getAlpha(curPixel);
curIndex++;
}
}

BufferedImage indexedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_BYTE_INDEXED, new IndexColorModel(resizedModel.pixel_bits, numPixels, reds, blues, greens, alphas));

虽然不知道这是否真的有效。

关于java - 在 Java 中调整索引图像的大小而不丢失透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6229378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com