上司给我发布的任务中有一个处理头像的需求做的时候因为种种需求发现需要转换图片的格式话不多说直接上代码
public static String PngPath(String path) {
BufferedImage bufferedImage;
try {
if (!path.substring(path.indexOf(".")).equals(".png")) {
bufferedImage = ImageIO.read(new File(path));
String imgName = path.substring(0, path.indexOf("."));
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
ImageIO.write(newBufferedImage, "png", new File(imgName + ".png"));
return imgName + ".png";
} else {
return path;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
我这直接转的png