Documentation

The Java™ Tutorials
Hide TOC
Working with Images处理图像
Trail: 2D Graphics

Lesson: Working with Images课程:使用图像

As you have already learned from the Images lesson, Images are described by a width and a height, measured in pixels, and have a coordinate system that is independent of the drawing surface.正如您已经从图像课程中了解到的,Image以宽度和高度(以像素为单位)进行描述,并且具有独立于图形表面的坐标系。

There are a number of common tasks when working with images.处理图像时有许多常见任务。

This lesson teaches you the basics of loading, displaying, and saving images.本课程将向您介绍加载、显示和保存图像的基本知识。

The are two main classes that you must learn about to work with images:要使用图像,您必须学习以下两个主要类:

The BufferedImage class is a cornerstone of the Java 2D immediate-mode imaging API. BuffereImage类是Java2D即时模式成像API的基石。It manages the image in memory and provides methods for storing, interpreting, and obtaining pixel data. 它管理内存中的图像,并提供存储、解释和获取像素数据的方法。Since BufferedImage is a subclass of Image it can be rendered by the Graphics and Graphics2D methods that accept an Image parameter.由于BuffereImageImage的一个子类,因此可以通过接受Image参数的GraphicsGraphics2D方法进行渲染。

A BufferedImage is essentially an Image with an accessible data buffer. BuffereImage本质上是具有可访问数据缓冲区的ImageIt is therefore more efficient to work directly with BufferedImage. 因此,直接使用BuffereImage更有效。A BufferedImage has a ColorModel and a Raster of image data. BuffereImage具有ColorModel和图像数据光栅The ColorModel provides a color interpretation of the image's pixel data.ColorModel提供图像像素数据的颜色解释。

The Raster performs the following functions:光栅执行以下功能:

The basic operations with images are represented in the following sections:以下部分介绍了图像的基本操作:

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

This section explains how to load an image from an external image format into a Java application using the Image I/O API本节介绍如何使用图像I/O API将图像从外部图像格式加载到Java应用程序中

Drawing an image画图

This section teaches how to display images using the drawImage method of the Graphics and Graphics2D classes.本节介绍如何使用GraphicsGraphics2D类的drawImage方法显示图像。

Creating and drawing To an image创建和绘制图像

This section describes how to create an image and how to use the image itself as a drawing surface.本节介绍如何创建图像以及如何将图像本身用作绘图曲面。

Writing/saving an image写入/保存图像

This section explains how to save created images in an appropriate format.本节介绍如何以适当的格式保存创建的图像。


Previous page: Previous Lesson
Next page: Reading/Loading an Image