Documentation

The Java™ Tutorials
Hide TOC
Reading/Loading an Image读取/加载图像
Trail: 2D Graphics
Lesson: Working with Images

Reading/Loading an Image读取/加载图像

When you think of digital images, you probably think of sampled image formats such as the JPEG image format used in digital photography, or GIF images commonly used on web pages. 当您想到数字图像时,您可能会想到采样图像格式,如数字摄影中使用的JPEG图像格式,或网页上常用的GIF图像。All programs that can use these images must first convert them from that external format into an internal format.所有可以使用这些图像的程序必须首先将它们从外部格式转换为内部格式。

Java 2D supports loading these external image formats into its BufferedImage format using its Image I/O API which is in the javax.imageio package. Java 2D支持使用javax.imageio包中的图像I/O API将这些外部图像格式加载到其BuffereImage格式中。Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. 图像I/O内置了对GIF、PNG、JPEG、BMP和WBMP的支持。Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats. 映像I/O也是可扩展的,因此开发人员或管理员可以“插件”支持其他格式。For example, plug-ins for TIFF and JPEG 2000 are separately available.例如,TIFF和JPEG 2000的插件是单独提供的。

To load an image from a specific file use the following code, which is from LoadImageApp.java:要从特定文件加载图像,请使用LoadImageApp.java中的以下代码:

BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}

Image I/O recognises the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.图像I/O将文件内容识别为JPEG格式的图像,并将其解码为可直接由Java 2D使用的BuffereImage

LoadImageApp.java shows how to display this image.显示如何显示此图像。

If the code is running in an applet, then its just as easy to obtain the image from the applet codebase. 如果代码在小程序中运行,那么从小程序代码库获取图像也同样容易。The following excerpt is from LoadImageApplet.java:以下摘录自LoadImageApplet.java

try {
    URL url = new URL(getCodeBase(), "examples/strawberry.jpg");
    img = ImageIO.read(url);
} catch (IOException e) {
}

The getCodeBase method used in this example returns the URL of the directory containing this applet when the applet is deployed on a web server. 当小程序部署到web服务器上时,本例中使用的getCodeBase方法返回包含该小程序的目录的URL。If the applet is deployed locally, getCodeBase returns null and the applet will not run.如果小程序部署在本地,getCodeBase将返回null,小程序将不会运行。

The following example shows how to use the getCodeBase method to load the strawberry.jpg file.下面的示例演示如何使用getCodeBase方法加载strawberry.jpg文件。


Note:  If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.如果您没有看到小程序正在运行,则至少需要安装JavaSE开发工具包(JDK)7版本。

LoadImageApplet.java contains the complete code for this example and this applet requires the strawberry.jpg image file.LoadImageApplet.java包含此示例的完整代码,此小程序需要strawberry.jpg图像文件。

In addition to reading from files or URLS, Image I/O can read from other sources, such as an InputStream. 除了从文件或URL读取外,图像I/O还可以从其他源(如InputStream)读取。ImageIO.read() is the most straightforward convenience API for most applications, but the javax.imageio.ImageIO class provides many more static methods for more advanced usages of the Image I/O API. 对于大多数应用程序来说,ImageIO.read()是最简单方便的API,但是javax.imageio.ImageIO类为更高级的图像I/O API使用提供了更多的静态方法。The collection of methods on this class represent just a subset of the rich set of APIs for discovering information about the images and for controlling the image decoding (reading) process.这个类上的方法集合只是一组丰富的API的子集,用于发现有关图像的信息和控制图像解码(读取)过程。

We will explore some of the other capabilities of Image I/O later in the Writing/Saving an Image section.稍后在编写/保存图像部分,我们将探讨图像I/O的一些其他功能。


Previous page: Working with Images
Next page: Drawing an Image