我正在处理的项目要求我从给定的url读取png图像;检索到的png图像具有透明的背景,我希望将其设置为白色。
以下是我所拥有的:
String u = this.format() ;
BufferedImage image = null ;
try{
URL url = new URL(u) ;
image = ImageIO.read(url) ;
Graphics2D graphics = image.createGraphics() ;
graphics.setBackground(Color.WHITE) ;
graphics.clearRect(0, 0, image.getWidth(), image.getHeight()) ;
ImageIO.write(image, "png", new File(outPath + fileName)) ;
graphics.dispose() ;
}
catch(IOException e){
e.printStackTrace() ;
}
我遇到的问题是,当我查看图像时,图像显示为纯白框。显然,我已经在我检索到的现有图像上覆盖了一个白色背景。
如何保留原始图像而只更改背景?或者先设置背景,然后覆盖检索到的图像?