gpt4 book ai didi

java - 将图像调整为固定分辨率后如何去除不必要的黑色背景?

转载 作者:行者123 更新时间:2023-11-29 06:00:58 25 4
gpt4 key购买 nike

在将每张图片添加到我的数据库之前,我一直将其调整为固定分辨率。为此,我一直在使用 FileUpload 和以下代码:-

logo_name = System.currentTimeMillis() + ".png";
File uploadedFile = new File("/www/static.appcanvas.com/"+logo_name);
BufferedImage bi = ImageIO.read(item.getInputStream());
Image img = bi.getScaledInstance(125,125,Image.SCALE_SMOOTH);
BufferedImage img_logo = new BufferedImage(125,125,BufferedImage.TYPE_INT_RGB);
Graphics2D g = img_logo.createGraphics();
g.drawImage(img,0,0,null);
if(g != null) g.dispose();
ImageIO.write(img_logo,"png",uploadedFile)

我得到了所需分辨率的图像,但有一个我无法删除的不必要的黑色背景。之前的图像:http://www.rocketcampus.com/images/test.png之后的图像:http://static.appcanvas.com/1334085929080.png

最佳答案

您可以将 BufferedImage 的类型更改为 BufferedImage.TYPE_INT_ARGB 以获得透明背景。

这对我有用:

public static void main(String[] args) throws IOException {
FileInputStream item = new FileInputStream("D:/tmp/OpenFlexo_07.gif");
String logo_name = System.currentTimeMillis() + ".png";
File uploadedFile = new File("d:/www/static.appcanvas.com/" + logo_name);
BufferedImage bi = ImageIO.read(item);
Image img = bi.getScaledInstance(125, 125, Image.SCALE_SMOOTH);
BufferedImage img_logo = new BufferedImage(125, 125, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img_logo.createGraphics();
g.drawImage(img, 0, 0, null);
if (g != null) {
g.dispose();
}
uploadedFile.getParentFile().mkdirs();
uploadedFile.createNewFile();
ImageIO.write(img_logo, "png", uploadedFile);

}

关于java - 将图像调整为固定分辨率后如何去除不必要的黑色背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10095182/

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