Java 如何将 BufferedImage 转换为 Image?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22923518/
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-13 19:03:12  来源:igfitidea点击:

How can I convert a BufferedImage to an Image?

javaimagebufferedimage

提问by dannYonkeys

I'm trying to split up an image of a deck of cards and store an individual card into an Image.

我正在尝试拆分一副纸牌的图像并将单张卡片存储到图像中。

Image image;
BufferedImage deck = ImageIO.read( new URL( "http://www.jfitz.com/cards/classic-playing-cards.png" ) );
image = deck.getSubimage( x, y, width, height );

incompatible types: BufferedImage cannot be converted to Image

不兼容的类型:BufferedImage 无法转换为 Image

Everywhere I've looked says that BufferedImage isan Image, this problem shouldn't exist. (Note: if this comes off looking like a class assignment, it is, but all we're supposed to do is load an image from a predetermined directory. I'm trying to take it a step further by using the URL for fun.)

我看过的所有地方都说 BufferedImage一个图像,这个问题不应该存在。(注意:如果这看起来像一个类分配,它是,但我们应该做的就是从预定的目录加载图像。我试图通过使用 URL 来更有趣。 )

EDIT 04/09/2014
I've been using different imports, so I guess the question I'm really trying to ask is how to convert a java.awt.image.BufferedImage to a java.scene.image.Image. Thanks for the help so far!

编辑 04/09/2014
我一直在使用不同的导入,所以我想我真正想问的问题是如何将 java.awt.image.BufferedImage 转换为 java.scene.image.Image。感谢你目前的帮助!

采纳答案by dannYonkeys

Once I knew what question to ask, google answered this really quickly. I found a quick fix on this blog.

一旦我知道要问什么问题,谷歌就很快回答了这个问题。我在这个博客上找到了一个快速修复。

BufferedImage tempCard = deck.getSubimage( x, y, width, height );
Image card = SwingFXUtils.toFXImage(tempCard, null );

Tried it out and works great!

试过了,效果很好!

回答by Chris Smith

Image image = new ImageIcon(bufferedImage).getImage();

That's the way I usually do it... OR

这就是我通常这样做的方式......或者

Image image = (Image) bufferedImage;

回答by Julian

A BufferedImage extends Image which means that you can declare a BufferedImage like so:

A BufferedImage 扩展 Image 这意味着您可以像这样声明 BufferedImage :

Image myImage = ImageIO.read(new URL("my/url"));

This starts getting into polymorphism and inheritance. Your object may be a BufferedImage, but a BufferedImage is still an Image and it can be used as one.

这开始进入多态和继承。您的对象可能是一个 BufferedImage,但 BufferedImage 仍然是一个 Image,它可以用作一个。

A more recognizable example perhaps would be instantiating an ArrayList as a List

一个更容易识别的例子可能是将一个 ArrayList 实例化为一个列表

List<MyType> aList = new ArrayList<>();

This allows this ArrayList to be compatible with all types of List objects, just like the BufferedImage.

这允许此 ArrayList 与所有类型的 List 对象兼容,就像 BufferedImage 一样。

Take a look at the API page for Image.

查看ImageAPI 页面

It lists all known subclasses of this abstract class, BufferedImage included. (There's only 2.)

它列出了这个抽象类的所有已知子类,包括 BufferedImage。(只有 2 个。)

回答by Shreyos Adikari

BufferedImageis a subclass of Image. You don't need to do any conversion.

BufferedImageImage的子类。您无需进行任何转换。

You can just assign like:

你可以像这样分配:

Image image;
BufferedImage deck = ImageIO.read( new URL( "http://www.jfitz.com/cards/classic-playing-cards.png" ) );
image = deck;// That will do