在后端开发的过程中,都逃不开与文件传输特别是图片的传输打交道,但是因为现在各种拍照设备发展越来越快,拍出的照片更是越来越清晰,但是照片文件的大小也是越来越大了,手机拍照小则2M大则30M这在网络传输过程中谁顶得住呀!所以在用户发布照片,后端对图像文件进行保存的过程中压缩图像文件是必不可少的一个过程。
而Thumbnails就是一个很好的图像处理工具,他把复杂的图像处理封装的很好,只需要短短的一行代码就能完成对图像的压缩。
Thumbnails支持:
Thumbnails导入依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
//size(宽度, 高度)
/*
* 若图片横比200小,高比300小,不变
* 若图片横比200小,高比300大,高缩小到300,图片比例不变
* 若图片横比200大,高比300小,横缩小到200,图片比例不变
* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
*/
Thumbnails.of("images/a380_1280x1024.jpg")
.size(200, 300)
.toFile("c:/a380_200x300.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(2560, 2048)
.toFile("c:/a380_2560x2048.jpg");
File file = new File("c:\\test.png");
Thumbnails.of(new FileInputStream(file)).scale(3.0).toFile(new File("c:\\yyyyy.png"));
3.0是一个double类型的数字,缩放比例,大于1就是变大,小于1就是缩小
//keepAspectRatio(false) 默认是按照比例缩放的
Thumbnails.of("images/a380_1280x1024.jpg")
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_200x200.jpg");
Thumbnails.of(new File("D:\\pics").listFiles()).scale(0.2).outputFormat("png")
.toFiles(Rename.PREFIX_DOT_THUMBNAIL);
File fromPic = new File("C:\\Users\\Administrator\\Desktop\\IdCardPositive_987136936_1531741954688.jpeg");
File toPic =new File("C:\\Users\\Administrator\\Desktop\\IdCardPositive_987136936_08.jpeg");
Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
outputQuality就是用来控制图片质量的
Thumbnails.of(fromPic).scale(0.8)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterPic), 0.5f)
.outputQuality(0.8f).toFile(toPic);
//watermark(位置,水印图,透明度)
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f)
.toFile("c:/a380_watermark_bottom_right.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f)
.toFile("c:/a380_watermark_center.jpg");
fromPic是原图,waterPic是水印图片,toPic是生成后的图片
Thumbnails.of(fromPic).scale(0.5).rotate(90).toFile(toPic);
Thumbnails.of(fromPic).sourceRegion(Positions.CENTER, 300, 300).scale(1.0).toFile(toPic);
//sourceRegion()
//图片中心400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(Positions.CENTER, 400,400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_center.jpg");
//图片右下400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(Positions.BOTTOM_RIGHT, 400,400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_bootom_right.jpg");
//指定坐标
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(600, 500, 400, 400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_coord.jpg");
某些应用上传的图片可能质量比较高,但是用户在列表浏览的时候,又不想原图展示,因为带宽要求较高,此时可以降低图片质量(上面提到的outputQuality),以outputstream输出流的方式response给浏览器去展示
@RequestMapping("/getImages")
public void getImages(HttpServletRequest request, HttpServletResponse response) throws IOException {
Thumbnails.of("images/a380_1280x1024.jpg").
scale(1f).
outputQuality(0.5f).
outputFormat("jpg").toOutputStream(response.getOutputStream());
}
//outputFormat(图像格式)
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.outputFormat("png")
.toFile("c:/a380_1280x1024.png");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.outputFormat("gif")
.toFile("c:/a380_1280x1024.gif");
//asBufferedImage() 返回BufferedImage
BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg"));
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
Thumbnails.of("images/a380_1280x1024.jpg").
scale(1f).
outputQuality(0.5f).
outputFormat("jpg").toOutputStream(thumbnailOutputStream);
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
Thumbnails.of("images/a380_1280x1024.jpg").
scale(1f).
outputQuality(0.5f).
outputFormat("jpg").toOutputStream(thumbnailOutputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
ByteArrayOutputStream handlerOutputStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).
scale(1f).
outputQuality(0.25f).
outputFormat("jpg").toOutputStream(handlerOutputStream);
byte[] bytes = handlerOutputStream.toByteArray();