Java 使用Thumbnails对大图片压缩
#代码知识 发布时间: 2026-01-12
引言

在最近的项目开发中,经常会使用到图片上传,但是过大的图片在查看的时候会影响打开速度,浪费流量以及服务器存储空间,所以需要在后端处理完图片再上传,这里我们用到了Thumbnails图片处理工具类。
Thumbnails主要支持以下一些功能
1、指定大小进行缩放
2、按照比例进行缩放
3、不按照比例,指定大小进行缩放
4、旋转
5、水印
6、裁剪
7、转化图片格式
8、输出到OutputStream
9、输出到BufferedImage
使用步骤:
一、添加Maven
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
二、具体操作
1:指定大小进行缩放
/**
* 指定大小进行缩放
*
* @throws IOException
*/
private void test1() throws IOException {
/*
* size(width,height) 若图片横比200小,高比300小,不变
* 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变
* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
*/
Thumbnails.of("images/test.jpg").size(200, 300).toFile("C:/image_200x300.jpg");
Thumbnails.of("images/test.jpg").size(2560, 2048).toFile("C:/image_2560x2048.jpg");
}
2:按照比例进行缩放
/**
* 按照比例进行缩放
* scale 图片的压缩比例 值在0-1之间,1f就是原图,0.5就是原图的一半大小 * outputQuality 图片压缩的质量 值在0-1 之间,越接近1质量越好,越接近0 质量越差
* @throws IOException
*/
private void test2() throws IOException {
/**
* scale(比例)
*/
Thumbnails.of("images/test.jpg").scale(0.25f).outputQuality(0.8f).toFile("C:/image_25%.jpg");
Thumbnails.of("images/test.jpg").scale(0.75f).outputQuality(0.8f).toFile("C:/image_110%.jpg"); }
3:不按照比例,指定大小进行缩放
/**
* 不按照比例,指定大小进行缩放
*
* @throws IOException
*/
private void test3() throws IOException {
/**
* keepAspectRatio(false) 默认是按照比例缩放的
*/
Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false).toFile("C:/image_120x120.jpg"); }
4:旋转
/**
* 旋转
*
* @throws IOException
*/
private void test4() throws IOException {
/**
* rotate(角度),正数:顺时针 负数:逆时针
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(90).toFile("C:/image+90.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(-90).toFile("C:/iamge-90.jpg");
}
5:水印
/**
* 水印
*
* @throws IOException
*/
private void test5() throws IOException {
/**
* watermark(位置,水印图,透明度)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_bottom_right.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");
}
6:裁剪
/**
* 裁剪
*
* @throws IOException
*/
private void test6() throws IOException {
/**
* 图片中心400*400的区域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_center.jpg");
/**
* 图片右下400*400的区域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_bootom_right.jpg");
/**
* 指定坐标
*/
Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");
}
7:转化图片格式
/**
* 转化图片格式
*
* @throws IOException
*/
private void test7() throws IOException {
/**
* outputFormat(图像格式)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("png").toFile("C:/image_1280x1024.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("gif").toFile("C:/image_1280x1024.gif");
}
8:输出到OutputStream
/**
* 输出到OutputStream
*
* @throws IOException
*/
private void test8() throws IOException {
/**
* toOutputStream(流对象)
*/
OutputStream os = new FileOutputStream("C:/image_1280x1024_OutputStream.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);
}
9:输出到BufferedImage
/**
* 输出到BufferedImage
*
* @throws IOException
*/
private void test9() throws IOException {
/**
* asBufferedImage() 返回BufferedImage
*/
BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280, 1024).asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("C:/image_1280x1024_BufferedImage.jpg"));
}
三、对图片文件进行Base64操作
/**
* 对内存中的图片文件进行Base64处理
*
* @throws IOException
*/
public String Base64ImageByMemory(BufferedImage pic) {
String imgString = "";
ByteArrayOutputStream newBaos = new ByteArrayOutputStream();//io流
try {
ImageIO.write(pic, "jpg", newBaos);//写入流中
byte[] bytes = newBaos.toByteArray();//转换成字节
imgString = URLEncoder.encode(new BASE64Encoder().encode(bytes), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return imgString;
}
四、获取服务器图片文件大小
/**
* 输出到OutputStream
*
* @throws IOException
*/
public void getImageFileSize(){
int size;
URLConnection conn;
try {
String path="";
path="https://bkimg.cdn.bcebos.com/pic/a8773912b31bb051c36044e93b7adab44bede0af";//世界地图
//path="http://10.30.23.217:9017/image/0c09ca36-abea-4efa-85b0-99b6d261f66c"; //服务器上图片
URL url = new URL(path);
conn = url.openConnection();
size = conn.getContentLength();
if (size < 0){
System.out.println("Could not determine file size.");
}else{
System.out.println("The size of file is = " + size + " bytes");
BigDecimal filesize = new BigDecimal(size);
BigDecimal megabyte = new BigDecimal(1024 * 1024);
float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
System.out.println("The size of file is = "+returnValue+"M");
}
conn.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
至此,图片压缩的相关处理和Base64以及获取服务器文件大小的功能就总结完了!
以上就是Java 使用Thumbnails对大图片压缩的详细内容,更多关于java 大图片压缩的资料请关注其它相关文章!
代码知识SEO上一篇 : nginx配置域名后的二级目录访问不同项目的配置操作
下一篇 : MySQL存储过程的深入讲解(in、out、inout)
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!