使用Thumbnailator,无论图像大小如何,我都能制作相同高度和宽度的缩略图 [英] Using Thumbnailator, can i make thumbnail with same height and width regardless the image size
问题描述
在 Thumbnailator 中,我正在制作缩略图。
In Thumbnailator, i am making thumbnails.
如果图像尺寸为400 * 300且如果我做了以下事情,
If image size is 400*300 and if i do following thing,
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
它创建了160 * 120的缩略图。
it create thumbnail of 160*120.
我想要的是如果我上传400 * 300图像,它将居中放大,这样我将变成300 * 300,然后它将缩略图。
What i want is if i upload 400*300 image, it will center zoom so that i will become 300*300 and then it will thumbnail.
我经历过文档,即使我在评论中发布相同的东西,但没有运气。
I gone through the documentation, Even i posted same thing over there in comment but no luck.
推荐答案
听起来像 sourceRegion
方法,可用于指定应从中生成缩略图的区域:
Sounds like a job for the sourceRegion
method which can be used to specify the region from which the thumbnail should be produced:
< a href =http://wiki.thumbnailator.googlecode.com/hg/img/features/sourceregion.png>使用< code> sourceRegion< / code>创建缩略图的插图Thumbnailator中的方法http://wiki.thumbnailator.googlecode.com/hg/img/features/sourceregion.png
在你的特殊情况下,你'我想尝试以下方法:
In your particular case, you'll want to try the following:
Thumbnails.of(new File("original.jpg"))
.sourceRegion(Positions.CENTER, 300, 300)
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
以上代码将:
- 打开
original.jpg
, - 使用原始图像的中央300 x 300区域,然后
- 将该区域的大小调整为160 x 160缩略图,
- 写入
thumbnail.jpg
。
- Open the
original.jpg
, - Use the central 300 x 300 region of the original image, and
- Resize that region to a 160 x 160 thumbnail, and
- Writes to the
thumbnail.jpg
.
通过更改 Positions.CENTER $ c,可以选择原始图像的不同区域例如,$ c>到
枚举。 Positions.TOP_LEFT
。有关预定义选项的完整列表,请查看 <的文档。 code>职位
It's possible to select different regions of the original image by changing Positions.CENTER
to, for example, Positions.TOP_LEFT
. For a complete list of pre-defined choices, please look at the documentation for the Positions
enum.
以下是一些链接可能感兴趣的Thumbnailator API文档:
The following are some links to the Thumbnailator API documentation which may be of interest:
-
sourceRegion(int,int,int,int)
方法
- 用于指定创建缩略图的确切区域。
- 使用相对定位使用
Position
对象,如上面的示例代码所示。
- Uses relative positioning using the
Position
object as shown in the example code above.
- 用于指定创建缩略图的确切区域,使用
java.awt.Rectangle
object。
- 提供预定义的位置,可用于指定创建缩略图的区域的相对位置。
免责声明:我是 Thumbnailator 库的维护者。
Disclaimer: I am the maintainer of the Thumbnailator library.
这篇关于使用Thumbnailator,无论图像大小如何,我都能制作相同高度和宽度的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!