Bufferedimage resize

Updated answer

I cannot recall why my original answer worked but having tested it in a separate environment, I agree, the original accepted answer doesn't work (why I said it did I cannot remember either). This, on the other hand, did work:

public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();

    return dimg;
}  

If all that is required is to resize a BufferedImage in the resize method, then the Thumbnailator library can do that fairly easily:

public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  return Thumbnails.of(img).size(newW, newH).asBufferedImage();
}

The above code will resize the img to fit the dimensions of newW and newH while maintaining the aspect ratio of the original image.

If maintaining the aspect ratio is not required and resizing to exactly the given dimensions is required, then the forceSize method can be used in place of the size method:

public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  return Thumbnails.of(img).forceSize(newW, newH).asBufferedImage();
}

Using the Image.getScaledInstance method will not guarantee that the aspect ratio of the original image will be maintained for the resized image, and furthermore, it is in general very slow.

Thumbnailator uses a technique to progressively resize the image which can be several times faster than Image.getScaledInstance while achieving an image quality which generally is comparable.

Disclaimer: I am the maintainer of this library.