繁体   English   中英

如何将图像转换为BufferedImage

[英]How to convert Image into BufferedImage

现在我有一个图像,我想将其转换为BufferedImage ...

这是我的代码-

private BufferedImage toBufferedImage(Image img, int width, int height){
    // Create a buffered image with transparency
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics bGr = bimage.getGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}

但这是行不通的。 有人可以解释什么地方吗?

BufferedImage扩展了Image,因此您可以投射它:

BufferedImage buffered = (BufferedImage) image;

但是您必须在运行时检查图像对象的类型,因为它可以包含其他类型的图像。

UPDATE

要将图像转换为BufferedImage,应检查此类并尝试:

http://www.dzone.com/snippets/converting-images

尝试包含JavaFX的新JRE。

您可以使用SwingFXUtils。 有一种方法可以满足您的要求:

BufferedImage image = SwingFXUtils.fromFXImage(fxImage, null);

要么

BufferedImage image = ImageIO.read(new File(filename));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM