gpt4 book ai didi

Java:生成具有透明度的缩略图

转载 作者:搜寻专家 更新时间:2023-11-01 03:45:27 25 4
gpt4 key购买 nike

我在生成带有 Alpha channel (透明度)的图像缩略图时遇到问题。我使用的代码是这样的:

public void saveThumbnail(File file, String imageType) {
if (bufferedThumb == null) {
return;
}

if(bufferedImage.getColorModel().hasAlpha()) {
logger.debug("Original image has Alpha channel");
}

BufferedImage bi = new BufferedImage(bufferedThumb.getWidth(null), bufferedThumb.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.getGraphics();
g.drawImage(bufferedThumb, 0, 0, null);
try {
ImageIO.write(bi, imageType, file);
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("Error occured saving thumbnail");
}
}

但是,如果我提供例如具有透明背景的 GIF 图像,我总是以黑色或彩色背景结束。

编辑:

这是使用缩略图从类中调用它的方式,我上次错过了 getThuimbnail() 方法的双参数版本:

Thumbnail th = new Thumbnail(file.getPath());
th.getThumbnail(100);

添加用于获取图像的方法:

public Thumbnail(String fileName) {
try {
this.bufferedImage = ImageIO.read(new File(fileName));
} catch (IOException ex) {
logger.error("Failed to read image file: " + ex.getMessage());
}
}

public Image getThumbnail(int size) {
int dir = VERTICAL;
if (bufferedImage.getHeight() < bufferedImage.getWidth()) {
dir = HORIZONTAL;
}
return getThumbnail(size, dir);
}

/**
* Creates image with specifed max sized to a specified direction.
* Will use Image.SCALE_SMOOTH for scaling.
* @param size Maximum size
* @param dir Direction of maximum size - 0 = vertical, 1 = height.
* @return Resized image.
*/
public Image getThumbnail(int size, int dir) {
return getThumbnail(size, dir, Image.SCALE_SMOOTH);
}

/**
* Creates image with specified size.
* @param size Maximum size
* @param dir Direction of maximum size - 0 = vertical, 1 = height.
* @param scale Image.Scale to use for conversion.
* @return Resized image.
*/
public Image getThumbnail(int size, int dir, int scale) {
if (dir == HORIZONTAL) {
bufferedThumb = bufferedImage.getScaledInstance(size, -1, scale);
} else {
bufferedThumb = bufferedImage.getScaledInstance(-1, size, scale);
}
return bufferedThumb;
}

谢谢!

最佳答案

缩小具有透明颜色的 GIF 图像应该可以正常工作。我同意 jarnbjo 的观点,这个问题最有可能出现在 bufferedThumb 一代。

也许以下提示会有所帮助:

1) 创建缩略图时从源复制图像类型,例如:

BufferedImage thumb = new BufferedImage(fit, fit, image.getType());

2) 使用二维方法:

Graphics2D g = thumb.createGraphics();

这里有一些简单的拇指创建示例代码(经过测试和工作;透明度 GIF,拇指保持透明):

  public static BufferedImage thumb(BufferedImage image, int fit) {

//image = blur(image);
BufferedImage thumb = new BufferedImage(fit, fit, image.getType());
Graphics2D g = thumb.createGraphics();

try {
int width = image.getWidth();
int height = image.getHeight();
int sx1;
int sy1;
int sx2;
int sy2;
int tmp;

if (height > width) {
tmp = height - width;
sx1 = 0;
sy1 = tmp / 2;
sx2 = width;
sy2 = height - sy1;
} else if (width > height) {
tmp = width - height;
sx1 = tmp / 2;
sy1 = 0;
sx2 = width - sx1;
sy2 = height;
} else {
sx1 = 0;
sy1 = 0;
sx2 = width;
sy2 = height;
}

g.drawImage(
image,
0, 0,
fit, fit,
sx1, sy1,
sx2, sy2,
null
);

} finally {
g.dispose();
}
return thumb;
}//thumb

注意:简单,我的意思是如果您试图在单个步骤中缩放太多(例如 2048 像素,100 像素),它不会产生高质量的结果。您可能需要采取多步骤方法,并且您可能应该求助于带有提示的 AffineTransformOp,而不是使用图形设备。

关于Java:生成具有透明度的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2155055/

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