简体   繁体   English

使用缩略图,无论图像大小如何,我都可以制作具有相同高度和宽度的缩略图吗

[英]Using Thumbnailator, can i make thumbnail with same height and width regardless the image size

In Thumbnailator , i am making thumbnails.Thumbnailator 中,我正在制作缩略图。

If image size is 400*300 and if i do following thing,如果图像大小为 400*300 并且我执行以下操作,

Thumbnails.of(new File("original.jpg"))
        .size(160, 160)
        .toFile(new File("thumbnail.jpg"));

it create thumbnail of 160*120.它创建 160*120 的缩略图。

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.我想要的是,如果我上传 400*300 的图像,它会居中缩放,这样我就会变成 300*300,然后它会缩略图。

I gone through the documentation, Even i posted same thing over there in comment but no luck.我浏览了文档,即使我在评论中发布了同样的内容,但没有运气。

Sounds like a job for the sourceRegion method which can be used to specify the region from which the thumbnail should be produced:听起来像是sourceRegion方法的一项工作,该方法可用于指定应从中生成缩略图的区域:

使用 Thumbnailator 中的 sourceRegion 方法创建缩略图的图示

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"));

The above code will:上面的代码将:

  1. Open the original.jpg ,打开original.jpg ,
  2. Use the central 300 x 300 region of the original image, and使用原始图像的中央 300 x 300 区域,以及
  3. Resize that region to a 160 x 160 thumbnail, and将该区域调整为 160 x 160 缩略图,然后
  4. Writes to the thumbnail.jpg .写入thumbnail.jpg

It's possible to select different regions of the original image by changing Positions.CENTER to, for example, Positions.TOP_LEFT .有可能通过改变选择的原始图像的不同区域Positions.CENTER ,例如, Positions.TOP_LEFT For a complete list of pre-defined choices, please look at the documentation for the Positions enum.有关预定义选项的完整列表,请查看Positions枚举的文档。


The following are some links to the Thumbnailator API documentation which may be of interest:以下是您可能感兴趣的缩略图 API 文档的一些链接:


Disclaimer: I am the maintainer of the Thumbnailator library.免责声明:我是缩略图库的维护者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使缩略图宽度Java - make a thumbnail image width java 将图像的高度和宽度减少50%,但磁盘上的图像大小保持不变 - Reducing image height and width by 50% but image size on disk remains the same 使用缩略图器将JPG转换为PNG时,图像尺寸增加了两倍 - Image size increased twice when convert from JPG to PNG using thumbnailator 如何获得图像的高度和宽度? - How can I get height and width of an image? 如何制作与面板相同尺寸的图像? - How can I make an image the same size as the panel? 如何使用java在给定新高度和宽度的情况下更改图像大小? - How to change the size of image with given new height and width using java? 如何在使用缩略图调整图像大小时用白色替换黑色背景? - How to replace black background with white on image resizing using Thumbnailator? 使用java,我如何使一个方法接受BufferedImage,旋转它,并返回一个BufferedImage(具有正确的宽度/高度) - Using java, how can I make a method taking in a BufferedImage, rotate it, and return a BufferedImage ( with correct width / height ) 我如何使用intelij idea获取Java中图像的宽度和高度? - How can i get the width and height of an image in java using intelij idea? 在java中使用thumbnailator裁剪图像 - Crop image in java with thumbnailator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM