views:

86

answers:

3

I resized an image using Java2D Graphics class. But it doesn't look right.

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();

Is it possible to scale an image without introducing artifacts?

+1  A: 

Bitmap graphics do not scale well, generally speaking. Degradation is particularly notable when you increase the size of the image, but even scaling down can introduce undesirable artifacts especially if not scaling by integral factors.

The best solution, if you need multiple sizes of a single image for display, is to either use vector* graphics or take the highest fidelity bitmap you have and scale down, and by integral factors.

*Note that vector graphics aren't an option for photographs and the like.

Kevin Montrose
A: 

This article by Chris Campbell has lots of detailed information on scaling images with Java2D.

There are a number of options you can use regarding the quality of the scaling, where generally the better the quality the longer the scaling will take (performance versus quality tradeoff).

The information in the article will probably help your scaling look better, but as @Kevin says in his answer, at the end of the day no scaling is going to be absolutely perfect.

Ash
A: 

You could look into java-image-scaling library. With a quick test it created a better quality down scaled image than using standard Java2D/AWT tools.

Juha Syrjälä