7

I have viewed this question, but it does not seem to actually answer the question that I have. I have a, image file, that may be any resolution. I need to load that image into a BufferedImage Object at a specific resolution (say, for this example, 800x800). I know the Image class can use getScaledInstance() to scale the image to a new size, but I then cannot figure out how to get it back to a BufferedImage. Is there a simple way to scale a Buffered Image to a specific size?

NOTE I I do not want to scale the image by a specific factor, I want to take an image and make is a specific size.

Community
  • 1
  • 1
ewok
  • 20,148
  • 51
  • 149
  • 254
  • **I do not want to scale the image by a specific factor, I want to take an image and make is a specific size** This can be done easily: `factor = originalSize\newSize;` – GETah Jul 06 '12 at 18:00

3 Answers3

9

Something like this? :

 /**
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * @param srcImg - source image to scale
 * @param w - desired width
 * @param h - desired height
 * @return - the new resized image
 */
private BufferedImage getScaledImage(Image srcImg, int w, int h){
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(srcImg, 0, 0, w, h, null);
    g2.dispose();
    return resizedImg;
}
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
4

You can create a new BufferedImage of the size you want and then perform a scaled paint of the original image into the new one:

BufferedImage resizedImage = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, new_width, new_height, null);
g.dispose();
Robert
  • 39,162
  • 17
  • 99
  • 152
1

see this website Link1

Or This Link2

codeDEXTER
  • 1,181
  • 7
  • 14
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Kalle Richter Dec 04 '16 at 21:30
  • Thanks for the input @KarlRichter . I'll definitely update the answer soon. – codeDEXTER Jan 24 '17 at 11:56