用J2SE1.4新特性来获取图象资源
下面的这个例子示范怎样使用javax.imageio包来从一个文件、输入流或URL获取图象资源,同时也示范了如何来把Image显示到屏幕上.
javax.imageio包默认支持GIF,PNG和JPEG格式的图片,这个例子只能工作在J2SE1.4或以上版本
Image image = null;
try {
File file = new File("image.gif");
image = ImageIO.read(file);
InputStream is = new BufferedInputStream(
new FileInputStream("image.gif"));
image = ImageIO.read(is);
URL url = new URL("image = ImageIO.read(url);
} catch (IOException e) {
}
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
转载:转载请保留本信息,本文来自http://www.51dibs.com/lp07/la/a6/l_a_6c55b1df7f70bb78.html
下面的这个例子示范怎样使用javax.imageio包来从一个文件、输入流或URL获取图象资源,同时也示范了如何来把Image显示到屏幕上.
javax.imageio包默认支持GIF,PNG和JPEG格式的图片,这个例子只能工作在J2SE1.4或以上版本
Image image = null;
try {
File file = new File("image.gif");
image = ImageIO.read(file);
InputStream is = new BufferedInputStream(
new FileInputStream("image.gif"));
image = ImageIO.read(is);
URL url = new URL("image = ImageIO.read(url);
} catch (IOException e) {
}
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
包javax.imageio默认可以读和写一个GIF,PNG和JPEG图片,你可以调用ImageIO.getReaderFormatNames() 和ImageIO.getWriterFormatNames()来列出全部可读和可写的图片格式
String[] formatNames = ImageIO.getReaderFormatNames();
formatNames = unique(formatNames);
formatNames = ImageIO.getWriterFormatNames();
formatNames = unique(formatNames);
// png jpeg jpg
formatNames = ImageIO.getReaderMIMETypes();
formatNames = unique(formatNames);
// image/jpeg image/png image/x-png image/gif
formatNames = ImageIO.getWriterMIMETypes();
formatNames = unique(formatNames);
// image/jpeg image/png image/x-png
public static String[] unique(String[] strings) {
Set set = new HashSet();
for (int i=0; I
String name = strings[i].toLowerCase();
set.add(name);
}
return (String[])set.toArray(new String[0]);
}