The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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 Java 2D支持使用BufferedImage
format using its Image I/O API which is in the javax.imageio
package. 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 图像I/O将文件内容识别为JPEG格式的图像,并将其解码为可直接由Java 2D使用的BufferedImage
which can be directly used by 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 当小程序部署到web服务器上时,本例中使用的getCodeBase
method used in this example returns the URL of the directory containing this applet when the applet is deployed on a web server. 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
文件。
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的一些其他功能。