Thumbnails一个非常好用的图像处理API,这个API基本可以实现图像的常用操作,获取图片信息、扩大、缩小、压缩大小、添加水印、裁剪、转换格式,还可以直接使用相应的linux命令。
Maven依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
获取Builder对象
Builder<File> bdFile = Thumbnails.of(files.getAbsolutePath())
应用场景
按比例缩放
/**
* @Title: scale<br>
* @Description: 按比例缩放<br>
* @param scale 缩放比例 :大于1是放大,小于1是缩小
* @param fromFile 源图片路径
* @param tofile 目的图片路径
* @throws IOException <br>
*/
public static void scale(double scale, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).scale(0.5).toFile(tofile);
}
按大小尺寸缩放
/**
* @Title: size<br>
* @Description: 按大小尺寸缩放<br>
* @param height 图片高 px
* @param width 图片宽 px
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void size(int height,int width, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).size(width,height).toFile(tofile);
}
按指定高度缩小图片,等比例调整图片宽度
/**
* @Title: height<br>
* @Description: 按指定高度缩小图片,等比例调整图片宽度<br>
* @param height 图片缩小高度至 px
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void height(int height, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).height(height).toFile(tofile);
}
按指定宽度缩小图片,等比例调整图片高度
/**
* @Title: width<br>
* @Description: 按指定宽度缩小图片,等比例调整图片高度<br>
* @param width 图片缩小宽度至 px
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void width(int width, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).width(width).toFile(tofile);
}
设置图片质量
/**
* @Title: outputQuality<br>
* @Description: 设置图片质量,一般与缩放功能同时使用<br>
* @param quality 图片质量比例,1 为高质量,越接近0图片质量越低
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void outputQuality(float quality, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).outputQuality(quality).toFile(tofile);
}
添加水印
/**
* @Title: watermark<br>
* @Description: 添加图片水印<br>
* @param position 位置
* @param image 图片
* @param opacity 透明度
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void watermark(Position position, BufferedImage image, float opacity, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).watermark(position, image, opacity).toFile(tofile);
}
这个水印方法可以满足一般需求,但定制化的还需要自己来添加了,方便就好。水印添加可以参考另一个方案。
定制化水印处理工具类
图片旋转
/**
* @Title: rotate<br>
* @Description: 图片旋转 <br>
* @param angle 正数则为顺时针,负数则为逆时针
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void rotate(double angle, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).rotate(angle).toFile(tofile);
}
图片裁剪
/**
* @Title: sourceRegion<br>
* @Description: 裁剪图片<br>
* @param position 图片位置
* @param width 裁剪宽
* @param height 裁剪高
* @param fromFile
* @param tofile
* @throws IOException <br>
*/
public static void sourceRegion(Position position, int width, int height, String fromFile, String tofile) throws IOException {
// Thumbnails.of(fromFile).sourceRegion(Positions.TOP_CENTER, width, height).toFile(tofile);
Thumbnails.of(fromFile).sourceRegion(position, width, height).toFile(tofile);
}
批处理图片文件
/**
* @Title: listfiles<br>
* @Description: 批量处理文件,可与前几个方法结合起来处理<br>
* @param fromDir
* @param tofile
* @throws IOException <br>
*/
public static void listfiles(String fromDir, String tofile) throws IOException {
Thumbnails.of(new File(fromDir).listFiles()).toFile(tofile);
}
Linux命令 批量处理压缩图片
find /thumbnail/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +49k -exec convert -resize "x300>" -quality 75 {} {} \;
DEMO
public static void main(String[] args) {
//转换图片测试
File f = new File("");
try {
//获取图片高
getBdFile(f.getAbsolutePath()).asBufferedImage().getHeight();
//按指定高度缩放图片,高质量输出
getBdFile(f.getAbsolutePath()).height(300).outputQuality(1).toFile(f.getAbsolutePath());;
//批量产生缩略图
Thumbnails.of(new File("H:/pics").listFiles())
.scale(0.2)
.outputFormat("jpg")
.toFiles(Rename.PREFIX_DOT_THUMBNAIL);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}//获取图片信息
}
其他方法可以自己直接尝试了,都在对象里面,看英文对照翻译一下就可以用起来
网站实现效果,使用了压缩,定高展示,高质量输出,由于网站图片量大,直接采用的linux命令来实现的批量缩略图处理
最后可以参考:https://blog.csdn.net/qq_30336433/article/details/81298154 这篇文章讲的挺全的