java 如何将 BufferedImage 转换/转换为图像?

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

How to cast/convert a BufferedImage into an Image?

javaimageawtitextbufferedimage

提问by Gundon

I would like to convert a BufferedImageto an java.awt.Image.

我想将 a 转换BufferedImagejava.awt.Image.

My source-image is tif so I use JAI to read it as PlanarImage:

我的源图像是 tif,所以我使用 JAI 将其读取为PlanarImage

PlanarImage source = JAI.create("fileload", IMG_DIR + tagImgName);

PlanarImage source = JAI.create("fileload", IMG_DIR + tagImgName);

I then save it as an objects attribute as a BufferedImage

然后我将它保存为对象属性作为 BufferedImage

tagImg = source.getAsBufferedImage();

tagImg = source.getAsBufferedImage();

For .pdf-Export (via iText) I need it as java.awt.Image

对于 .pdf-Export(通过 iText),我需要它作为 java.awt.Image

Thanks!

谢谢!

回答by Juha Syrj?l?

java.awt.image.BufferedImageis already a subclass of java.awt.Image, so you shouldn't need any casting or converting. You can use the BufferedImage in place of a Image.

java.awt.image.BufferedImage中已经是一个子类java.awt.Image中,所以你不需要任何铸造或转换。您可以使用 BufferedImage 代替 Image。

You can do directly this:

你可以直接这样做:

Image tagImg = source.getAsBufferedImage();
// use tagImg with iText library here

回答by Jon Skeet

Assuming you mean java.awt.image.BufferedImage, it already subclasses java.awt.image.Image- so you shouldn't need to do anything.

假设您的意思是java.awt.image.BufferedImage,它已经子类化了java.awt.image.Image- 所以您不需要做任何事情。