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 ); ...