在前端的页面开发过程,图片的压缩一直是相当重要的一件事。之前使用grunt的图片压缩插件,感觉没啥实际的效果。最后,根据从网上找的资料,发现https://tinypng.com/这个网站的图片压缩效果是非常好的。如果是几张图片,那么通过网页操作就可以进行了。
这里介绍的是如果基于Java API来进行批量图片压缩。
在调用SDK前需要获得一个appKey,用邮箱注册就好了。
获取appKey地址:https://tinypng.com/developers
具体使用步骤:
1、引入TinyPng的jar包,我使用maven导入
<dependency>
<groupId>com.tinify</groupId>
<artifactId>tinify</artifactId>
<version>RELEASE</version>
</dependency>
2、注册APP Key
public class Example {
public static void main(String[] args) {
Tinify.setKey("YOUR_API_KEY"); // Tiny官网注册一个就好了
}
}
3、压缩图片
Source source = Tinify.fromFile("unoptimized.jpg");
source.toFile("optimized.jpg");
4、压缩并调整图片大小
Source source = Tinify.fromFile("large.jpg");
Options options = new Options()
.with("method", "fit")
.with("width", 150)
.with("height", 100);
Source resized = source.resize(options);
resized.toFile("thumbnail.jpg");
method总共有scale、fit以及cover三种方式:
scale:按比例缩放图片,必须提供高度或者宽度,不需要同时提供两者。
fit:让图片在指定的大小内,需要同时提供高度和宽度,会有一边存在留白的内容。
cover:剪切图片到指定大小,需要同时提供高度和宽度,系统根据图片自动剪切出有价值的部分。
5、批量压缩图片
这个类是用来批量压缩图片资源的,能够对一个目录下的图片进行批量压缩。
public class TinyPngMain {
public static void main(String[] args) throws IOException {
String appKey = ""; // Tiny官网注册一个就好了
Tinify.setKey(appKey);
float scale = 1.0f;
scale = 1.5f;
String src = "D:/imgs/normal";
String dest = "D:/imgs/x1.5";
tinyFold(src, dest, Boolean.FALSE, Boolean.FALSE, scale);
}
public static void tinyFold(String srcFoldPath, String destFoldPath, boolean recursion, boolean keepStructure, float scale) throws IOException {
File srcFold = new File(srcFoldPath);
if (srcFold.isFile()) {
System.out.println(String.format("%s===>%s", srcFoldPath, destFoldPath));
tinyOneFile(srcFoldPath, destFoldPath, scale);
return;
}
File destFold = new File(destFoldPath);
if (!destFold.exists()) {
destFold.mkdir();
}
File[] subFiles = srcFold.listFiles();
if (subFiles == null || subFiles.length == 0) {
return;
}
for (File subFile: subFiles) {
if (subFile.isDirectory()) {
if (recursion) {
// 需要迭代
String recurisonDestFoldPath = destFoldPath;
if (keepStructure) {
// 保持结构不变
String foldName = subFile.getName();
recurisonDestFoldPath = String.format("%s/%s", destFoldPath, foldName);
}
tinyFold(subFile.getAbsolutePath(), recurisonDestFoldPath, recursion, keepStructure, scale);
}
} else {
// 如果是文件那么dest带上文件名
String name = subFile.getName();
String destFilePath = String.format("%s/%s", destFoldPath, name);
tinyFold(subFile.getAbsolutePath(), destFilePath, recursion, keepStructure, scale);
}
}
}
public static void tinyOneFile(String filePath, String outputPath, float scale) throws IOException {
if (scale == 1.0f) {
Source source = Tinify.fromFile(filePath);
source.toFile(outputPath);
} else {
Source source = Tinify.fromFile(filePath);
Image image = ImageIO.read(new File(filePath));
int width = image.getWidth(null);
int realWidth = new BigDecimal(((double) width / scale)).setScale(0, BigDecimal.ROUND_HALF_UP).toBigInteger().intValue();
// System.out.println(new File(filePath).getName() + ": " + realWidth);
Options options = new Options()
.with("method", "scale")
.with("width", realWidth);
Source resized = source.resize(options);
resized.toFile(outputPath);
}
}
}