开发者

resizing image java getScaledInstance

开发者 https://www.devze.com 2023-03-31 18:12 出处:网络
here is my code\" ImageIcon ii=new ImageIcon(\"/Users/tushar_chutani/Desktop/apple.jpg\"); Image image= ii.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);

here is my code"

ImageIcon ii=new ImageIcon("/Users/tushar_chutani/Desktop/apple.jpg");  

Image image= ii.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);

the image is not being scaled what is开发者_StackOverflow中文版 wrong with the code?


The problem is that Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.

For example, if you use the scaled image in a Graphics2D.drawImage() call then the method will return false and continue drawing asynchronously. You then have to use the ImageObserver parameter in the Graphics2D.drawImage() call to wait for completion of the scaling and drawing.

The following example shows how to scale images more simply without an ImageObserver. The scaling is done by drawing the icon into a BufferedImage instead.

import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.RenderingHints;

public class Tushar2
{
        public void scaleImage()
        {
                try
                {
                        ImageIcon ii = new ImageIcon("/tmp/apple.jpg");
                        BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = (Graphics2D)bi.createGraphics();
                        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,
                                RenderingHints.VALUE_RENDER_QUALITY));
                        boolean b = g2d.drawImage(ii.getImage(), 0, 0, 50, 50, null);
                        System.out.println(b);
                        ImageIO.write(bi, "jpg", new File("/tmp/apple50.jpg"));
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
        }

        public static void main(String []args)
        {
                new Tushar2().scaleImage();
        }
}


You can wrap the image in an image icon again. An image icon is generally loading the image in its constructor and uses its own media tracker for this purpose. I am using the following code now:

     Image image = icon.getImage().getScaledInstance(
          icon.getIconWidth() * NEW / OLD,
          icon.getIconHeight() * NEW / OLD,
          Image.SCALE_SMOOTH);
     icon = new ImageIcon(image, icon.getDescription());

You can directly work with the new icon, or call getImage() to work with the new image. The above code does also an aspect ratio preserving scaling given on some NEW and OLD pair.

Bye


Your code:

ImageIcon ii=new 
ImageIcon("/Users/tushar_chutani/Desktop/apple.jpg");  

Add this one:

Image image= ii.getImage().getScaledInstance(50, 50, 
Image.SCALE_SMOOTH);
ii=new ImageIcon(image);

IF YOU WANT TO display : Add this..

jLabel1.setIcon(ii);
0

精彩评论

暂无评论...
验证码 换一张
取 消