How to scale image using Java
In this post we will see how can we scale an image to a desired dimension.
In the following code snippet the scaleImage method is responsible for scaling the source image to the given height & width. Instead of overwriting the original file the code creates a new file for the scaled image version.
public static String scaleImage(String imgSrc, int width, int height, String dest){
try{
File f = new File(imgSrc);
//Reads the Image from the given URL
BufferedImage img = ImageIO.read(f);
//Scales the BufferedImage to the desired dimensions
Image scaledImg = img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
//'coz the Image object cannot be written to disk directly
//We have to recreate a new BufferedImage instance from the scaled Image instance
BufferedImage biScaledImg = toBufferedImage(scaledImg, BufferedImage.TYPE_INT_RGB);
//Writes the file on to the disk at the given destination
ImageIO.write(biScaledImg, "jpeg", new File(dest));
}
catch(IOException e){
return "Exception occurred while scaling the Image. Exception: "+e.getMessage();
}
return dest;
}
/**
* Method to create BufferedImage Instance from given Image Object
*/
private static BufferedImage toBufferedImage(Image image, int type) {
int w = image.getWidth(null);
int h = image.getHeight(null);
BufferedImage result = new BufferedImage(w, h, type);
Graphics2D g = result.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return result;
}
You can download the complete source code from here
It should be noted that if the new height & width is greater than the source image height & width then the resultant image will be a pixelated image.
In the following code snippet the scaleImage method is responsible for scaling the source image to the given height & width. Instead of overwriting the original file the code creates a new file for the scaled image version.
public static String scaleImage(String imgSrc, int width, int height, String dest){
try{
File f = new File(imgSrc);
//Reads the Image from the given URL
BufferedImage img = ImageIO.read(f);
//Scales the BufferedImage to the desired dimensions
Image scaledImg = img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
//'coz the Image object cannot be written to disk directly
//We have to recreate a new BufferedImage instance from the scaled Image instance
BufferedImage biScaledImg = toBufferedImage(scaledImg, BufferedImage.TYPE_INT_RGB);
//Writes the file on to the disk at the given destination
ImageIO.write(biScaledImg, "jpeg", new File(dest));
}
catch(IOException e){
return "Exception occurred while scaling the Image. Exception: "+e.getMessage();
}
return dest;
}
/**
* Method to create BufferedImage Instance from given Image Object
*/
private static BufferedImage toBufferedImage(Image image, int type) {
int w = image.getWidth(null);
int h = image.getHeight(null);
BufferedImage result = new BufferedImage(w, h, type);
Graphics2D g = result.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return result;
}
You can download the complete source code from here
It should be noted that if the new height & width is greater than the source image height & width then the resultant image will be a pixelated image.
Comments
Post a Comment