Java中Image和BufferedImage的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3944825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:20:41  来源:igfitidea点击:

Difference between the Image and BufferedImage in Java

javaimagebufferedimage

提问by Tofiq

What is the difference between Image and BufferedImage?

Image 和 BufferedImage 有什么区别?

Can I create a BufferedImage directly from an Image source "image.png"?

我可以直接从图像源“image.png”创建一个 BufferedImage 吗?

采纳答案by Alvin

If you are familiar with Java's util.List, the difference between Image and BufferedImage is the same as the difference between List and LinkedList.

如果你熟悉Java的util.List,Image和BufferedImage的区别和List和LinkedList的区别是一样的。

Image is a generic concept and BufferedImage is the concrete implementation of the generic concept; kind of like BMW is a make of a Car.

Image是泛型概念,BufferedImage是泛型概念的具体实现;有点像宝马是汽车制造商。

回答by Marcus Adams

Image is an abstract class. You can't instantiate Image directly. BufferedImage is a descendant, and you can instantiate that one. So, if you understand abstract classes and inheritance, you'll understand when to use each.

Image 是一个抽象类。您不能直接实例化 Image。BufferedImage 是一个后代,您可以实例化它。因此,如果您了解抽象类和继承,您就会明白何时使用它们。

For example, if you were using more than one Image descendant, they're going to share some common properties, which are inherited from Image.

例如,如果您使用多个 Image 后代,它们将共享一些从 Image 继承的公共属性。

If you wanted to write a function that would take either kind of descendant as a parameter you could do something like this:

如果您想编写一个将任一后代作为参数的函数,您可以执行以下操作:

function myFunction(Image myImage) {
  int i = myImage.getHeight();
  ...
}

You could then call the function by passing it a BufferedImage or a VolatileImage.

然后,您可以通过传递 BufferedImage 或 VolatileImage 来调用该函数。

BufferedImage myBufferedImage;
VolatileImage myVolatileImage;
...
myFunction(myVolatileImage);
myFunction(myBufferedImage);

You won't convert an Image to a BufferedImage because you'll never have an Image.

您不会将 Image 转换为 BufferedImage,因为您永远不会拥有 Image。

回答by Paul Samsotha

What is the difference between Imageand BufferedImage?

Image和 和有BufferedImage什么区别?

As stated in the Oracle Java tutorial for working with Images

如使用图像的 Oracle Java 教程中所述

  • The java.awt.Image class is the superclass that represents graphical images as rectangular arrays of pixels.
  • The java.awt.image.BufferedImage class, which extends the Image class to allow the application to operate directly with image data (for example, retrieving or setting up the pixel color). Applications can directly construct instances of this class.
  • java.awt.Image 类是将图形图像表示为像素的矩形阵列的超类。
  • java.awt.image.BufferedImage 类,它扩展了 Image 类以允许应用程序直接操作图像数据(例如,检索或设置像素颜色)。应用程序可以直接构造此类的实例。

The BufferedImage class is a cornerstone of the Java 2D immediate-mode imaging 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.

A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with BufferedImage.A BufferedImage has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image's pixel data.

BufferedImage 类是 Java 2D 即时模式成像 API 的基石。它管理内存中的图像并提供存储、解释和获取像素数据的方法。由于 BufferedImage 是 Image 的子类,因此它可以由接受 Image 参数的 Graphics 和 Graphics2D 方法呈现。

BufferedImage 本质上是一个具有可访问数据缓冲区的图像。因此,直接使用 BufferedImage 效率更高。BufferedImage 有一个 ColorModel 和一个 Raster 图像数据。ColorModel 提供图像像素数据的颜色解释。



Can I create a BufferedImagedirectly from an Image source "image.png"?

我可以BufferedImage直接从图像源“image.png”创建一个吗?

Sure.

当然。

BufferedImage img = ImageIO.read(getClass().getResource("/path/to/image"));