2013-08-31 102 views
0

我想將URL中的圖像大小調整爲50x50像素。Java從URL調整圖像大小?

這是我如何加載圖像:

jLabel1.setIcon(new javax.swing.ImageIcon(new URL("http://url.com/picture.jpg"))); 

我怎樣才能讓picture.jpg 50x50px?

任何想法?

回答

1

你可以嘗試這樣的事:

public BufferedImage resize(final URL url, final Dimension size) throws IOException{ 
    final BufferedImage image = ImageIO.read(url); 
    final BufferedImage resized = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); 
    final Graphics2D g = resized.createGraphics(); 
    g.drawImage(image, 0, 0, size.width, size.height, null); 
    g.dispose(); 
    return resized; 
} 

用法:

final BufferedImage image = resize(new URL("http://url.com/picture.jpg"), new Dimension(50, 50)); 
3

我會讓你組裝3件拼圖。