How to make ImageIO read from InputStream :Java

前端 未结 2 598
温柔的废话
温柔的废话 2021-01-05 14:05

I have created executable jar file(using Eclipse) , there are a set of image (.png) files that is to be inculded in the jar. So I have added a source folder with all the ima

2条回答
  •  伪装坚强ぢ
    2021-01-05 14:54

    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);
    

提交回复
热议问题