This utility offers quick image scaling transformations based on initial parameters and selected via method transformation type.
The main utility object instance can be initialed with the original image data using any of the following supported types: InputStream
, BufferedImage
, byte[]
and Path
.
Once the main object is initialized, a transformation method should be invoked. There are several scaling transofmations available: Fit, Expand, Adjust, Limit and Tile (please see the examples of each scaling transformation type below).
After the transformation is done, user can call any of the available convience methods to obtain/save the resulting image.
Usage examples:
// save scaled image into local file system from an input stream
new ImageScaler(inputStream)
.fit(200, 200, new int[] { 192, 205, 224 })
.saveAs(ImageFormat.JPG, Paths.get("/tmp"), "scaledImage");
// get scaled BufferedImage from an image in local file system
final BufferedImage scaledImage = new ImageScaler(Paths.get("/tmp/originalImage.jpg"))
.adjust(200, 200)
.getImage();
// get scaled image bytes from image bytes
final byte[] scaledImageBytes = new ImageScaler(originalImageBytes)
.fit(200, 200)
.toByteArray(ImageFormat.PNG);
// get scaled image base64 encoded string from a BufferedImage
final String htmlReadyScaledImageData = new ImageScaler(originalBufferedImage)
.limit(Dimension.WIDTH, 200)
.encode(ImageFormat.JPG);
Scales image fitting and centering it onto the output canvas with its original ratio conserved leaving unoccupied canvas space filled with the specified background color (white by default)). This can be particularly useful when the user needs to standardize the output dimmensions and at the same time maintain the original image uncut.
Original | Fit example 1 | Fit example 2 |
---|---|---|
Scales image to the target dimensions. When target ratio is different from the original one an image distortion may occur. Useful when the user just needs to scale the image without maintaining its ratio.
Original | Exapnd example |
---|---|
Scales image to the max target dimension while maintaining its original ratio. In case output ratio is different from the original one the image is centered and cropped. Useful when the user needs to standardize the output dimmensions without having monotone borders.
Original | Adjust example |
---|---|
Scales image fitting it within the limit defined in pixels along the specified dimension. Useful for setting a dimmensional limit for images without loosing their ratio.
Original | Limit example |
---|---|
Uses input image as a tile filling the output image with the original image copies as they fit. Isn't particularly useful but what the hell =).
Original | Tile example |
---|---|
If you're feeling lazy and just want to grab the latest JARs, they are here:
Project api docs are available here: API reference