Quality loss when loading image into ImageIcon

Hi,

I have this piece of code that creates an ImageIcon to be set as Icon for a JLabel. The problem is that the image quality is reduced to multi-uglyness and I can't figure out how to add just the ImageIcon without loss of precision.

Thanks for your help !

Coenos

/**

* Gets the image Icon of a certain imageLocation

*

* @param imageLocation

* @param width

* @param height

* @return

*/

public static ImageIcon getImageIcon(String imageLocation, int width, int height) {

// create imageIcon

ImageIcon imageIcon = new ImageIcon(imageLocation);

Image image = imageIcon.getImage().getScaledInstance(

width, height, 0);

// scale image

imageIcon.setImage(image);

return imageIcon;

}

[814 byte] By [Coenosiaa] at [2007-10-3 3:43:52]
# 1

getScaledInstance(width, height, 0)

There are five scaling algorithm flags ranging from 1 to 16 that can be used as hints in getScaledInstance. These are listed in the method detail for the method in the Image class api. Follow the link for one of them to the field detail and on to the Constant_Field_Values for Image to find the int values for each static constant.

If you want good quality you might try Image.SCALE_AREA_AVERAGING (16). This method works well for scaling to smaller size and is slow. If you are scaling up or want more speed you can try working with BufferedImage, AffineTransform scale instance and the Graphics2D rendering hint value RenderingHints.VALUE_INTERPOLATION_BICUBIC.

74philipa at 2007-7-14 21:39:57 > top of Java-index,Security,Cryptography...
# 2

Hi 74philip,

thanks for your reply, I see indeed the getScaledImage hints in the javadoc. Could you give me a code snippet example of using BufferedImage, AffineTransform scale instance and the Graphics2D rendering hint value in relation to ImageIcon ?

I have tried to get a working example but none of them use ImageIcon within a JLabel.

Thanks !

Coen

Coenosiaa at 2007-7-14 21:39:57 > top of Java-index,Security,Cryptography...
# 3
Would this help? http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_How_do_I_create_a_resized_copyDmitri
dmitri_trembovetskia at 2007-7-14 21:39:57 > top of Java-index,Security,Cryptography...
# 4

import java.awt.*;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class QuickScale

{

private JPanel getContent(BufferedImage image)

{

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(image)));

BufferedImage scaled = getScaledImage(image, 175, 140);

panel.add(new JLabel(new ImageIcon(scaled)));

return panel;

}

private BufferedImage getScaledImage(BufferedImage src, int w, int h)

{

BufferedImage dest = new BufferedImage(w, h, src.getType());

Graphics2D g2 = dest.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);

// fill background for scale to fit

g2.setPaint(UIManager.getColor("Panel.background"));

g2.fillRect(0, 0, w, h);

double xScale = (double)w / src.getWidth();

double yScale = (double)h / src.getHeight();

double scale = Math.min(xScale, yScale);// scale to fit

//Math.max(xScale, yScale); // scale to fill

double x = (w - scale*src.getWidth())/2;

double y = (h - scale*src.getHeight())/2;

AffineTransform at = AffineTransform.getTranslateInstance(x, y);

at.scale(scale, scale);

g2.drawRenderedImage(src, at);

g2.dispose();

return dest;

}

public static void main(String[] args) throws IOException

{

File file = new File("images/cougar.jpg");

BufferedImage bi = ImageIO.read(file);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new QuickScale().getContent(bi));

f.pack();

f.setLocation(200,200);

f.setVisible(true);

}

}

74philipa at 2007-7-14 21:39:57 > top of Java-index,Security,Cryptography...
# 5

Hi,

I have tried getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING) but this decreases performance considerably. Even for smaller sized images.

Is the BufferedImage implementation faster with remain of quality ? Or is there no way to improve performance as well as quality ?

Thanks !

Coen

Coenosiaa at 2007-7-14 21:39:57 > top of Java-index,Security,Cryptography...