Search code examples
javaimageresizescalebufferedimage

Resize image without loss of information


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?


Solution

  • 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.