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?!
Edit
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?
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
Canvasfilter which can perform cropping and padding (or letterboxing) of resulting thumbnails.Padding an image
For example, using the
Canvasfilter to pad an image can be achieved by the following:The above will:
sizemethod.addFiltermethod 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
Canvasfilter can be achieved by the following:The above code will:
sizemethod.trueargument that is present in theCanvasconstructor 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
cropmethod which should reduce the need for calling theaddFiltermethod under most circumstances.