主要有三个步骤:
- 读取png图片
- 填充背景色为白色
- 保存成jpg图片
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.net.URL;
import java.io.File;
import javax.imageio.ImageIO;
public static void main(String[] args) {
try {
//1.读取本地png图片or读取url图片
File input = new File("/Users/wuxinyue/XXamPFXa.png");
BufferedImage bimg = ImageIO.read(input);//读取本地图片
//BufferedImage bimg = ImageIO.read(new URL("http://img.alicdn.com/tfs/TB1kbMoUOLaK1RjSZFxXXamPFXa.png"));//读取url图片
//2. 填充透明背景为白色
BufferedImage res = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_INT_RGB);
res.createGraphics().drawImage(bimg, 0, 0, Color.WHITE, null); //背景填充色设置为白色,也可以设置为其他颜色比如粉色Color.PINK
//3. 保存成jpg到本地
File output = new File("/Users/wuxinyue/white_bg.jpg");
ImageIO.write(res, "jpg", output);
} catch (Exception e) {
e.printStackTrace();
}
}
reference:
https://memorynotfound.com/convert-png-to-jpg-image-file-using-java/
来源:CSDN
作者:adrianna_xy
链接:https://blog.csdn.net/u012223913/article/details/103481900