e694. reading an image from a file, inputstream, or url.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 30 行
TXT
30 行
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 + =
减小字号Ctrl + -
显示快捷键?