Image resizing with Java2D and quality loss

O

OtisUsenet

Hi,

I need to scale / resize an image and create a thumbnail out of it.
I'm using the Java2D approach (i.e. using only Java's image
manipulation API without external libraries), but I'm getting
thumbnail images that are of visibly lower quality than originals (and
significantly lower quality than when I resize images with, say, a
commandline ImageMagick tool (mogrify).

Is there *any* way not to lose image quality *significantly* when
resizing images with Java2D?

Here is the code I'm currently using:

public static BufferedImage scaleImage(InputStream srcImage, int
width, int height) throws IOException {
BufferedImage bsrc = ImageIO.read(srcImage);
BufferedImage bdest = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

Graphics2D g = bdest.createGraphics();
AffineTransform at =
AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
(double)height/bsrc.getHeight());
g.drawRenderedImage(bsrc, at);
return bdest;
}


Any help or pointers would be appreciated. Thank you!
 
D

Daniel Pitts

Hi,

I need to scale / resize an image and create a thumbnail out of it.
I'm using the Java2D approach (i.e. using only Java's image
manipulation API without external libraries), but I'm getting
thumbnail images that are of visibly lower quality than originals (and
significantly lower quality than when I resize images with, say, a
commandline ImageMagick tool (mogrify).

Is there *any* way not to lose image quality *significantly* when
resizing images with Java2D?

Here is the code I'm currently using:

public static BufferedImage scaleImage(InputStream srcImage, int
width, int height) throws IOException {
BufferedImage bsrc = ImageIO.read(srcImage);
BufferedImage bdest = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

Graphics2D g = bdest.createGraphics();
AffineTransform at =
AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
(double)height/bsrc.getHeight());
g.drawRenderedImage(bsrc, at);
return bdest;
}

Any help or pointers would be appreciated. Thank you!

You might try using some of the rendering hints in Graphics2D, or try
using Image.getScaledInstance(at, Image.SCALE_SMOOTH);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top