使用ImageIO.read(URL)或ImageIO.read(File)代替。加载时它将阻塞,返回后将知道图像的宽度和高度。
例如
import java.awt.image.BufferedImage;import javax.swing.*;import javax.imageio.ImageIO;import java.net.URL;class SizeOfImage { public static void main(String[] args) throws Exception { URL url = new URL("https://i.stack.imgur.com/7bI1Y.jpg"); final BufferedImage bi = ImageIO.read(url); final String size = bi.getWidth() + "x" + bi.getHeight(); SwingUtilities.invokeLater(new Runnable() { public void run() { JLabel l = new JLabel( size, new ImageIcon(bi), SwingConstants.RIGHT ); JOptionPane.showMessageDialog(null, l); } }); }}
或者,MediaTracker向由异步加载的映像中添加A ,Toolkit然后等待其完全加载。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)