Java – get bufferedimage from and OutputStream

I have a function, int readfull (filehandle, OutputStream out), which reads the entire file from the ssh server and stores it in the local stream I can write a file locally for the second parameter using fileoutputstream, and then read the file into the buffered image using the following:

bufferedImage = ImageIO.read(new File("/path/to/file"));

But how do I create a buffered image without first writing it to a file? I read this question, but I still can't figure out my situation

Solution

Just write to bytearrayoutputstream – you can then create a bytearrayinputstream to read the same byte array and pass it to imageio read(InputStream).

ByteArrayOutputStream output = new ByteArrayOutputStream();
readFully(handle,output);
byte[] data = output.toByteArray();
ByteArrayInputStream input = new ByteArrayInputStream(data);
BufferedImage image = ImageIO.read(input);

If you can't create an InputStream directly from filehandle, it will be easier

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>