Image Scaling With imgscalr – Java Image Scaling Library

21 / Jul / 2015 by Arpit Jain 0 comments

imgscalr is a simple and efficient image scaling library implemented purely in java and provides a handful of operations for image manipulation. This library can be useful in creating thumbnail or preview images of various sizes from larger uploaded images or performing basic image manipulation operation, incuding scaling, cropping, rotation etc. imagescalr is a set of most commonly used operations optimized for better performance & speed and is not meant to be a full-scale Java graphics library like JAI.

Installation:

If you are using grails then you can use it simply by adding following dependency to your BuildConfig.groovy file dependencies section:

[code language=”groovy”]
compile "org.imgscalr:imgscalr-lib:4.2" //adjust version according to your need
[/code]

and if you are using maven then add the following dependency to pom.xml:

[code language=”xml”]
<dependencies>

<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependencies>
[/code]

Usage:

In its simplest form you can resize image by passing just two arguments: your image (BufferedImage) and target size (in pixels).

[code language=”groovy”]
BufferedImage scaledImage = Scalr.resize(image, 250)
[/code]

Here, we are resizing our image (maintaining its original proportion) to a width and height no bigger than target size.

If you wanted finer grained control over image scaling, its quality and a light anti-aliasing filter to it then your method call will look something like this:

[code language=”groovy”]
BufferedImage scaledImage = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, targetWidth, targetHeight, Scalr.OP_ANTIALIAS)
[/code]

Here, Scalr.Method.QUALITY indicates that the scaling implementation should do everything it can to create as nice of a result as possible, Scalr.Mode.FIT_TO_WIDTH indicates that the scaling implementation should calculate dimensions for the resultant image that best-fit within the given width, regardless of the orientation of the image and Scalr.OP_ANTIALIAS is a ConvolveOp using a very light “blur” kernel that acts like an anti-aliasing filter (softens the image a bit) when applied to an image.

Method and Mode are the enums defined on the Scalr class and are used in conjunction with all of the resize methods.

Given below is the code snippet in grails that you can use as a reference:

[code language=”groovy”]
import org.imgscalr.Scalr
import javax.imageio.ImageIO
import java.awt.image.BufferedImage

def scaleImage() {
String pathToOriginalImage = "/home/tothenew/Desktop/image.jpg"
String imageFormat = "jpg"
Integer targetWidth = 400
Integer targetHeight = 600

File originalImageFile = new File(pathToOriginalImage)
BufferedImage image = ImageIO.read(originalImageFile)

BufferedImage scaledImg = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH,
targetWidth, targetHeight, Scalr.OP_ANTIALIAS)

ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(scaledImg, imageFormat, baos)
baos.flush()
byte[] scaledImageInByte = baos.toByteArray()
baos.close()
return scaledImageInByte
}
[/code]

If you want to dive into more depth, go to below url:

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *