1

I'm having some problems understanding this line of code:

bufferedImage.getScaledInstance(100, -1, Image.SCALE_SMOOTH);

I use it to resize an image with following numbers. Width: 3264px, height: 2448px, horizontal & vertical resolution: 150dpi, size: 2.72MB.

Then I save the bufferedImage into a File using

ImageIO.write(bufferedImage, "jpg", outputFile);

It resulted in a picture with the following numbers: Same width & height (3264x2448), horizontal & vertical resolution: 96dpi, size: 674KB.

Isn't the picture suppose to be 100px in width, and whatever height to maintain aspect ratio (according to Javadoc)? And why would the result pic have the same width & height, but different hor&ver resolution, and size? Am I missing something, cause it seems like no matter what value of width & height, the result pic is always the same as described above.

I've also read that I should draw the resized BufferedImage into a Graphics2D, and save the Graphics2D into file. Is that the proper way to do it, rather than saving the BufferedImage straight into file?

The image I'm using is here

buræquete
  • 14,226
  • 4
  • 44
  • 89
Tien Phan
  • 55
  • 9
  • Check its [documentation](https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance(int,%20int,%20int)), it says "If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions". I guess it fails to maintain the same aspect ratio with 1px width, and resets to original dimensions. – buræquete May 02 '17 at 08:07
  • Aspect ratio is maintained, however, dimension should not be. In my case, dimension is also maintained, while I'm trying to resize it. – Tien Phan May 02 '17 at 08:10
  • 2
    You are getting the response of the `.getScaledInstance()` right? It does not modify the `bufferedImage`, but returns the scaled image instance. If not, you might be writing the non-scaled image, and due to usage of `"jpg"` you might get the resolution change in the output image. – buræquete May 02 '17 at 08:17
  • Oh no I totally did not. I thought it scales it directly. Guess that's why u have to use Graphics2D, to work with the returned Image. I'll try it & update this thread. Thank you very much! – Tien Phan May 02 '17 at 08:29
  • You don't need `Graphics2D`. `getScaledInstance()` returns an `Image` object, just try to get its response, and `write()` on that object. `Image scaledImage = bufferedImage.getScaledInstance(100, -1, Image.SCALE_SMOOTH);` and `ImageIO.write(scaledImage, "jpg", outputFile);` – buræquete May 02 '17 at 08:30
  • You can't write an `Image` object though, `ImageIO.write()` requires `RenderedImage` – Tien Phan May 02 '17 at 08:33
  • You are right, sorry, use the following [answer](http://stackoverflow.com/a/19506998/3641067), to get `BufferedImage` from the response of `getScaledInstance()`, and `write()` that! – buræquete May 02 '17 at 08:35
  • It works, only thing is the quality is low. I'm looking at ways to increase the quality. And yeah post an answer so I can up-vote you (not sure if I can due to low reputation, but I'll try). Thanks again! – Tien Phan May 02 '17 at 19:08

1 Answers1

0

You are not getting the response of the .getScaledInstance(). It does not modify the bufferedImage, but returns the scaled image instance. Due to the usage of "jpg" you might get the resolution change in the output image.

So you'd have to do the following;

Image scaledImage = bufferedImage.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
// map Image to BufferedImage
ImageIO.write(scaledBufferedImage, "jpg", outputFile);

To create a BufferedImage from the scaled Image, use the following answer.

Community
  • 1
  • 1
buræquete
  • 14,226
  • 4
  • 44
  • 89