How to scale BufferedImage

I looked through this question , but actually it does not answer the question that I have. I have an image file that can be of any resolution. I need to load this image into a BufferedImage object with a specific resolution (for example, 800x800 for this example). I know that the Image class can use getScaledInstance() to scale the image to a new size, but then I cannot figure out how to get it back to BufferedImage . Is there an easy way to scale a buffered image to a specific size?

NOTE I do not want to scale the image by a certain ratio, I want to take the image and make a specific size.

+6
source share
3 answers

Something like that?

  /** * 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; } 
+8
source

You can create a new BufferedImage of the desired size, and then scale the original image to a 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(); 
+4
source

see this site Link1

Or is it Link2

+1
source

Source: https://habr.com/ru/post/919891/


All Articles