We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
我维护 Thumbnailator,这是一个 Java 缩略图生成库,它提供了调整图像大小和执行一些简单图像操作的方法通过易于使用的流畅的 API 进行操作。
Thumbnailator 提供的功能之一是
Canvas
过滤器,可以执行裁剪和填充(或信箱)生成的缩略图。填充图像
例如,使用
Canvas
滤镜填充图像可以通过以下方式实现:上面的操作将:
size
方法。addFilter
方法指定的附加过滤步骤将添加蓝色填充(使用Color.blue
)以生成尺寸为 150 x 150 的最终图像。path/to/padded-image.jpg
。在肖像图片上使用上述代码会产生以下结果:
(来源:coobird.net)
裁剪图像
使用
Canvas
滤镜裁剪图像可以通过以下方式实现:上面的代码将:
size
方法。Canvas
构造函数调用中存在的true
参数表示,如果图像大于指定尺寸,则应裁剪图像。)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:The above will:
size
method.addFilter
method will add a blue padding (usingColor.blue
) to result in an final image with the dimensions 150 x 150.path/to/padded-image.jpg
.Using the above code on a portrait picture results in the following:
(source: coobird.net)
Cropping an image
Cropping an image with the
Canvas
filter can be achieved by the following:The above code will:
size
method.true
argument that is present in theCanvas
constructor call indicates that an image should be cropped if larger than the specified dimensions.)path/to/cropped-image.jpg
.An example of running the above code will be the following:
(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 theaddFilter
method under most circumstances.你可以试试这个:
You can try this: