1

I was trying to do really the same functionality of resizing images like in MS Word.

I want to resize BufferedImage but I’m losing some information during process of resizing.

I tried to implement two approaches, but both produced same result.

Before any resizing:

enter image description here

Picture after few resize actions in my application:

enter image description here

First approach:

image = Thumbnails.of(image).size(w,h).asBufferedImage();

Second approach:

image = toBufferedImage(image.getScaledInstance(w, h, Image.SCALE_SMOOTH));

image is instance of BufferedImage, w is new width of image and h is new hight of image

Any idea, what I’m doing wrong?

1 Answers1

0

You're constantly losing information from your image after each resizing attempt. If you want solution (like in MS Word) you have to keep somewhere original image but show only resized copy.

The best solution would be creating an object to store original image and making resized copy on demand. You can improve quality of this solution adding simple cache so you don't actually generate another resized copy of your image for every request but only if your application demands image with different height or width than last time.

I hope I helped you a bit.

Trynkiewicz Mariusz
  • 2,722
  • 3
  • 21
  • 27
  • Additionally, read this[1] article for better ways to resize any image. [1] - https://www.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html – Brett Okken Mar 01 '15 at 19:04
  • Yes, `Image.getScaledInstance()` is asynchronous and, furthermore, is way slower than existing alternatives. – Trynkiewicz Mariusz Mar 01 '15 at 19:17
  • 1
    It was not easy, but it works. After memorize the original, is needed to memorize the aspect ration of last size of image. I made it with this simple algorithm: http://www.coderanch.com/t/467505/java/java/Resize-image-maintaing-aspect-ratio – Jan Brzobohatý Mar 01 '15 at 19:59