今天网上下载了几个webp格式的图片,想转换成png,下载的工具转换都收费,用Java怒写程序,自己转换,说干就干。
添加依赖
<!-- 支持图片格式 WebP -->
<dependency>
<groupId>org.sejda.imageio</groupId>
<artifactId>webp-imageio</artifactId>
<version>0.1.6</version>
</dependency>
代码
public static void main(String[] args) {
try {
String directoryPath = "C:\\Users\\Administrator\\Desktop\\abc";
File directory = new File(directoryPath);
if (directory.exists() && directory.isDirectory()) {
File[] files = directory.listFiles();
int i = 1;
for (File file : files) {
// 读取WebP图片文件
BufferedImage webpImage = ImageIO.read(file);
// 输出PNG图片文件,注意指定格式名称,即使文件扩展名不是.png
ImageIO.write(webpImage, "png", new File(directoryPath+"\\"+(i++)+".png"));
}
System.out.println("WebP to PNG conversion completed successfully.");
}
} catch (IOException e) {
System.out.println("Error converting WebP to PNG: " + e.getMessage());
}
}
这回心情舒畅,第一次感觉自己干的工作,用到了自己实际生活中,非常哇塞的程序员。