如何将缓冲图像转换为图像,反之亦然? [英] How to convert buffered image to image and vice-versa?
问题描述
其实我正在处理一个图像编辑软件,现在我想转换缓冲图像,即:
Actually i am working on a image editing software and now i want to convert the buffered-image i.e :
BufferedImage buffer = ImageIO.read(new File(file));
到图像,即格式如下:
Image image = ImageIO.read(new File(file));
有可能吗?如果是,那么如何?
Is it possible to so?? If yes, then how??
推荐答案
BufferedImage 是一个(n)图像,所以隐含您在第二行中执行的转换可以直接编译。如果你知道一个Image真的是一个BufferedImage,你必须像这样明确地转换它:
BufferedImage is a(n) Image, so the implicit cast that you're doing in the second line is able to be compiled directly. If you knew an Image was really a BufferedImage, you would have to cast it explicitly like so:
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
因为BufferedImage扩展了Image,所以它可以放在Image容器中。但是,任何Image都可以适合那里,包括那些不是BufferedImage的Image,因此如果类型不匹配,你可能会在运行时获得ClassCastException,因为BufferedImage除非扩展BufferedImage,否则不能保存任何其他类型。
Because BufferedImage extends Image, it can fit in an Image container. However, any Image can fit there, including ones that are not a BufferedImage, and as such you may get a ClassCastException at runtime if the type does not match, because a BufferedImage cannot hold any other type unless it extends BufferedImage.
这篇关于如何将缓冲图像转换为图像,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!