How to make ImageIO read from InputStream :Java

强颜欢笑 提交于 2019-12-04 05:24:24
CoderCroc

ImageIO.read() takes InputStream as a parameter so there is no meaning of casting it to ImageInputStream.

Secondly you can not cast an InputStreamReader object to ImageInputStream because ImageInputStream has nothing to do with InputStreamReader which you thought of.

Moreover getResourceAsStream() returns InputStream. So you can directly do it like this.

InputStream resourceBuff = YourClass.class.getResourceAsStream(filepath);
BufferedImage bf = ImageIO.read(resourceBuff);

The ImageIO class has a utility method to read an InputStream and create a BufferedImage.

There is also a utility method to create an ImageInputStream from an InputStream.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!