📄 e694. reading an image from a file, inputstream, or url.txt
字号:
This example demonstrates how to use the javax.imageio package to read an image from a file, input stream, or URL. The example also demonstrates how to display the image on the screen.
GIF, PNG, and JPEG formats are supported by the javax.imageio package by default.
This example works only in J2SE 1.4. For a method that works since J2SE 1.3, see e594 Reading an Image or Icon from a File.
Image image = null;
try {
// Read from a file
File file = new File("image.gif");
image = ImageIO.read(file);
// Read from an input stream
InputStream is = new BufferedInputStream(
new FileInputStream("image.gif"));
image = ImageIO.read(is);
// Read from a URL
URL url = new URL("http://hostname.com/image.gif");
image = ImageIO.read(url);
} catch (IOException e) {
}
// Use a label to display the image
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -