直接上转换工具方法代码,干货,自己copy
/**
* jpg to png
* @param jpgPath 需要被转换的jpg全路径带文件名
* @param pngPath 转换之后png的全路径带文件名
*/
public static void jpg2png(String jpgPath, String pngPath) {
try {
BufferedImage jpg = ImageIO.read(new File(jpgPath));
BufferedImage png =new BufferedImage(jpg.getWidth(), jpg.getHeight(), BufferedImage.TYPE_INT_RGB);
png.createGraphics().drawImage(jpg,0,0, Color.white,null);
ImageIO.write(png, "png", new File(pngPath));
} catch (Exception e) {
e.printStackTrace();
log.error("jpg转png失败!");
}
}
/**
* png to jpg
* @param jpgPath 转换之后jpg的全路径带文件名
* @param pngPath 需要被转换的png全路径带文件名
*/
public static void jpg2png(String jpgPath, String pngPath) {
try {
BufferedImage png= ImageIO.read(new File(pngPath));
BufferedImage jpg=new BufferedImage(png.getWidth(), png.getHeight(), BufferedImage.TYPE_INT_RGB);
jpg.createGraphics().drawImage(png,0,0, Color.white,null);
ImageIO.write(jpg, "jpg", new File(jpgPath));
} catch (Exception e) {
e.printStackTrace();
log.error("png转jpg失败!");
}
}