首先获取密钥:访问https://tinify.cn/developers
名字可以随便输,然后再输入你的邮箱,之后会接收邮件如下:
点击查看仪表盘前往第三方链接,密钥在下图所示
接下来是代码部分:
- 引入依赖
<dependency>
<groupId>com.tinify</groupId>
<artifactId>tinify</artifactId>
<version>RELEASE</version>
</dependency>
- 编写代码
public static void main(String[] args) throws Exception {
Tinify.setKey("B7tbXfDM2Pd3JrPjxzvb5mLbYrW8WW1C");
String path = "C:/Users/18702/Desktop/test/巴列霍.png";
String toPath = "C:/Users/18702/Desktop/test/1.png";
Source source = Tinify.fromFile(path);
source.toFile(toPath);
}
第一个路径是图片所在路径,第二个是压缩后的图片路径
其效果如下: