我应该使用什么java库进行图像裁剪/ letterboxing? [英] What java library should I use for image cropping/ letterboxing?

查看:137
本文介绍了我应该使用什么java库进行图像裁剪/ letterboxing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个Java库来进行图像裁剪/调整大小。我曾计划使用jmagick,但自2009年以来似乎没有维护过。

I'm looking for a Java library to do image cropping / resizing. I had planned to use jmagick, but it doesn't seem to have been maintained since 2009.

这是最好用的库吗?任何建议?!

Is this the best library to use? Any recommendations?!

编辑

我想做的一件事是能够填充图像以调整大小以及裁剪它。即如果我有4x2图像,并且我想使其成为正方形,我想将其制作为4x4,每边都有黑色或白色填充。
这在图像处理中是否有名称?它是任何库附带的函数吗?

One thing I want to do is to be able to pad an image to resize as well as crop it. i.e. if I have a 4x2 image, and I want to make it a square, I want to make it 4x4, with black or white padding at each side. Does this have a name in image manipulation? Is it a function that comes with any libraries?

推荐答案

我维护 Thumbnailator ,一个用于Java的缩略图生成库,它提供了调整图像大小并通过易于使用的流畅的API

I maintain Thumbnailator, a thumbnail generating library for Java, which provides means to resize images and do some simple image manipulations via a easy-to-use fluent API.

Thumbnailator提供的功能之一是 Canvas 过滤器,可以对生成的缩略图执行裁剪和填充(或letterboxing)。

One of the features that Thumbnailator provides is the Canvas filter which can perform cropping and padding (or letterboxing) of resulting thumbnails.

填充图像

例如,使用 Canvas 过滤器来填充图像可以通过以下方式实现:

For example, using the Canvas filter to pad an image can be achieved by the following:

Thumbnails.of("path/to/image.jpg")
  .size(150, 150)
  .addFilter(new Canvas(150, 150, Positions.CENTER, Color.blue))
  .toFile("path/to/padded-image.jpg");

以上将:


  1. 拍摄原始图像并通过尺寸方法将其缩小到150 x 150以内。

  2. 然后,由 addFilter 方法指定的附加过滤步骤将添加蓝色填充(使用 Color.blue )以产生最终结果图片尺寸为150 x 150。

  3. 将生成的缩略图保存到 path / to / padded-image.jpg

  1. Take an original image and shrink it to fit within 150 x 150 via the size method.
  2. Then, an additional filtering step specified by the addFilter method will add a blue padding (using Color.blue) to result in an final image with the dimensions 150 x 150.
  3. Save the resulting thumbnail to path/to/padded-image.jpg.

在肖像照片上使用上述代码会产生以下结果:

Using the above code on a portrait picture results in the following:

填充图片http://coobird.net/img/so-8150276-padding.jpg

裁剪图像

使用 Canvas 过滤器可以通过以下方式实现:

Cropping an image with the Canvas filter can be achieved by the following:

Thumbnails.of("path/to/image.jpg")
  .size(150, 150)
  .addFilter(new Canvas(100, 100, Positions.TOP_RIGHT, true))
  .toFile("path/to/cropped-image.jpg");

以上代码将:


  1. 拍摄原始图像并通过尺寸方法将其缩小到150 x 150以内。

  2. 然后,一个额外的过滤步骤将从调整大小的图像的右上角裁剪出一个100 x 100的区域。 ( Canvas 构造函数调用中出现的 true 参数表示如果图像大于指定值,则应裁剪图像尺寸。)

  3. 将生成的缩略图保存到 path / to / cropped-image.jpg

  1. Take an original image and shrink it to fit within 150 x 150 via the size method.
  2. Then, an additional filtering step will crop out a 100 x 100 region from the top-right hand corner of the resized image. (The true argument that is present in the Canvas constructor call indicates that an image should be cropped if larger than the specified dimensions.)
  3. Save the resulting thumbnail to path/to/cropped-image.jpg.

运行上述代码的示例如下:

An example of running the above code will be the following:

裁剪图片http://coobird.net/img/so-8150276-cropping.jpg

目前有功能要求将裁剪作为Thumbnailator API不可或缺的一部分,因此将来我计划添加 crop 方法,它可以减少在大多数情况下调用 addFilter 方法的需要。

There are currently feature requests to make cropping a more integral part of the Thumbnailator API, so in the future I am planning to add a crop method which should reduce the need for calling the addFilter method under most circumstances.

这篇关于我应该使用什么java库进行图像裁剪/ letterboxing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆