我应该使用什么 Java 库来进行图像裁剪/信箱处理?

发布于 2024-12-16 02:05:29 字数 1539 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-12-23 02:05:29

我维护 Thumbnailator,这是一个 Java 缩略图生成库,它提供了调整图像大小和执行一些简单图像操作的方法通过易于使用的流畅的 API 进行操作。

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

填充图像

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

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 通过 size 方法。
  2. 然后,由 addFilter 方法指定的附加过滤步骤将添加蓝色填充(使用 Color.blue)以生成尺寸为 150 x 150 的最终图像。
  3. 保存生成的缩略图为 path/to/padded-image.jpg

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

填充图像
(来源:coobird.net

裁剪图像

使用 Canvas 滤镜裁剪图像可以通过以下方式实现:

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 通过 size 方法。
  2. 然后,额外的过滤步骤将从调整大小的图像的右上角裁剪出 100 x 100 的区域。 (Canvas 构造函数调用中存在的 true 参数表示,如果图像大于指定尺寸,则应裁剪图像。)
  3. 将生成的缩略图保存到 path /to/cropped-image.jpg

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

裁剪图像
(来源:coobird.net


有目前,功能要求使裁剪成为 Thumbnailator API 中更不可或缺的一部分,因此将来我计划添加一个 crop 方法,该方法应该减少调用大多数情况下使用 addFilter 方法。

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.

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

Padding an image

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

The above will:

  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:

padded image
(source: coobird.net)

Cropping an image

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

The above code will:

  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:

cropped image
(source: coobird.net)


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.

北音执念 2024-12-23 02:05:29

你可以试试这个:

BufferedImage image=ImageIO.read(new FileInputStream("<youFile.jpg>"));
int min=0;
if(image.getWidth()>image.getHeight())
    min=image.getHeight();
else
    min=image.getWidth();

Thumbnails.of(image)
    .sourceRegion(Positions.CENTER, min, min)
    .size(250, 250)
  .toFile("<outputFile.jpg>");

You can try this:

BufferedImage image=ImageIO.read(new FileInputStream("<youFile.jpg>"));
int min=0;
if(image.getWidth()>image.getHeight())
    min=image.getHeight();
else
    min=image.getWidth();

Thumbnails.of(image)
    .sourceRegion(Positions.CENTER, min, min)
    .size(250, 250)
  .toFile("<outputFile.jpg>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文