java转换webp

准备工作

其实将图片转换为webp并不难,只需要调用google提供的转换器即可.转换器可以从这里下载.

转换器bin目录中包括以下工具 
- cwebp:将图片转换为webp格式 
- dwebp:解码webp 

- gif2webp:gif动图转换为webp格式

代码


package com.test;
public class ImageFormatConverter {
/**

* @param inputFile
*            原图片路径
* @param outputFile
*            webp路径
* @param quality
*            图片质量1-100
* @return
*/
private static boolean executeCWebp(String inputFile, String outputFile, Integer quality) {
boolean result = false;
ClassLoader cl = ImageFormatConverter.class.getClassLoader();
// 换成相应的路径
String cwebpPath = cl.getResource("com/test/cwebp.exe").getPath();
System.out.println("cwebpPath=" + cwebpPath);
try {
StringBuilder command = new StringBuilder(cwebpPath);
command.append(" -q " + (quality == 0 ? 75 : quality));
command.append(" " + inputFile);
command.append(" -o " + outputFile);
Runtime.getRuntime().exec(command.toString());
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}


public static void main(String[] args) {
String inputFile = "H:\\image\\新建文件夹\\test.jpg";
String outputFile = "H:\\image\\新建文件夹\\test.webp";
if (executeCWebp(inputFile, outputFile, 75)) {
System.out.println("convert ok~");
} else {
System.out.println("sth wrong happened");
}
}

}

本文是参考的https://blog.csdn.net/lx542657544/article/details/77871524


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值