Image manipulation problems.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackraven1425
    New Member
    • Mar 2008
    • 27

    Image manipulation problems.

    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?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by blackraven1425
    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?
    I've never had that problem. I find quality-wise, getScaledInstan ce is better for shrinking images and AffineTransform s better for magnifying images:

    [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

    Working...