I;m trying to scale an image (using the imgae's getScaledInstan ce method) before passing it to an ImageIcon, but it looks like the images are cropped. Has anyone had this problem before? Might it scale one side to the right side then crop the remainder on the other?
Image manipulation problems.
Collapse
X
-
Tags: None
-
I've never had that problem. I find quality-wise, getScaledInstan ce is better for shrinking images and AffineTransform s better for magnifying images:Originally posted by blackraven1425I;m trying to scale an image (using the imgae's getScaledInstan ce method) before passing it to an ImageIcon, but it looks like the images are cropped. Has anyone had this problem before? Might it scale one side to the right side then crop the remainder on the other?
[CODE=Java]import java.awt.*;
import java.awt.geom.* ;
import java.awt.image. *;
import java.io.*;
import java.net.*;
import javax.imageio.* ;
import javax.swing.*;
public class ImageResizingEx ample {
public static void main(String[] args) throws IOException {
URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");
BufferedImage image = ImageIO.read(ur l);
if (image == null) throw new NullPointerExce ption();
Image smaller = scaledInstance( image, 0.5);
BufferedImage blurry = rescaled(image, AffineTransform Op.TYPE_NEAREST _NEIGHBOR, 1.5);
BufferedImage better = rescaled(image, AffineTransform Op.TYPE_BICUBIC , 1.5);
display(image, smaller, blurry, better);
}
static Image scaledInstance( BufferedImage im, double factor) {
int w = (int) (im.getWidth() * factor);
int h = (int) (im.getHeight() * factor);
return im.getScaledIns tance(w, h, Image.SCALE_ARE A_AVERAGING);
}
static BufferedImage rescaled(Buffer edImage im, int hint, double factor) {
AffineTransform xform = AffineTransform .getScaleInstan ce(factor, factor);
return new AffineTransform Op(xform, hint).filter(im , null);
}
static void display(final Image... images) {
EventQueue.invo keLater(new Runnable(){
public void run() {
JPanel cp = new JPanel(new GridLayout(0,2, 5, 5));
for(Image image : images) {
cp.add(new JLabel(new ImageIcon(image )));
}
JFrame f = new JFrame("ImageRe sizingExample") ;
f.setContentPan e(cp);
f.setDefaultClo seOperation(JFr ame.EXIT_ON_CLO SE);
f.pack();
f.setLocationRe lativeTo(null);
f.setVisible(tr ue);
}
});
}
}[/CODE]
Comment