How to get an InputStream from a BufferedImage?

前端 未结 3 1551
后悔当初
后悔当初 2020-12-08 00:05

How can I get an InputStream from a BufferedImage object? I tried this but ImageIO.createImageInputStream() always returns NULL

BufferedImage bigImage = Grap         


        
3条回答
  •  孤城傲影
    2020-12-08 01:03

    From http://usna86-techbits.blogspot.com/2010/01/inputstream-from-url-bufferedimage.html

    It works very fine!

    Here is how you can make an InputStream for a BufferedImage:

    URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif");
    BufferedImage image = ImageIO.read(url);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(image, "gif", os);
    InputStream is = new ByteArrayInputStream(os.toByteArray());
    

提交回复
热议问题