Thumbnails 对图片进行先缩放,再剪裁

本文介绍了如何使用Thumbnailator库通过Canvas滤镜进行图片缩放并精确裁剪,以达到100x100的固定尺寸,同时保持原始比例。重点讲解了如何通过`newCanvas`方法配合大小和居中位置来实现定制缩略图需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Thumbnails 对图片进行先缩放,再剪裁

Thumbnails 首先对图片进行缩放,缩放之后再进行剪裁出想要的大小
Thumbnails.of(inputStream).size(width,height).addFilter(new Canvas(width, height, Positions.CENTER)).outputQuality(0.8f).toOutputStream(response.getOutputStream());
主要是new Canvas(width, height, Positions.CENTER) 这个做剪裁
参考: https://github.com/coobird/thumbnailator/issues/22
关键原文:
If what is desired is to merely crop an image, Thumbnailator does not provide a 
direct way to do so, however there is a `Canvas` image filter which can perform 
image cropping. (Please refer the javadocs at 
http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/filters
/Canvas.html)

> 3. I will add empty area around to have exactly 100x100

The aforementioned `Canvas` image filter has a feature to forcefully make 
thumbnails a certain size.

For example, say we take a 200x400 image and scale it down to 50x100, then we 
want to have an empty area to make a 100x100 thumbnail. The code can be written 
as follows:

  Thumbnails.of("path/to/image-200x400")
    .size(100, 100)
    .addFilter(new Canvas(100, 100, Positions.CENTER))
    .toFile("path/to/thumbnail");

Note: `size(100, 100)` will preserve the aspect ratio of the original, so the 
thumbnail without the `Canvas` filter would be 50x100.

https://github.com/springcloud-zym/minio-util

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值