gpt4 book ai didi

java - 如何使用java从图像中去除白色矩形

转载 作者:行者123 更新时间:2023-11-29 03:54:12 25 4
gpt4 key购买 nike

我厌倦了下面的代码来生成缩略图,但是当我把它放在另一个图像上时,缩略图有一个白色的矩形。如何使用 java graphics 2D 删除它?

  Image image = javax.imageio.ImageIO.read(new File(originalFile));       
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
File file = new File(thumbnailFile);
if(javax.imageio.ImageIO.write(thumbImage, "png", file))
return file;
}

即使我删除这三行,我也会得到黑色矩形。

 graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);

如何让这张图片变透明?请有人帮助我。

最佳答案

 Color transparent = new Color(0,0,0,0)

通过设置 Compositehere : AlphaComposite

 // Create an image that supports arbitrary levels of transparency
Graphics2D g2d = (Graphics2D) image.getGraphics();
BufferedImage thumbImage = g2d.getDeviceConfiguration().createCompatibleImage(
thumbWidth, thumbHeight, Transparency.TRANSLUCENT);

关于java - 如何使用java从图像中去除白色矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7293849/

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