Java – convert bufferedimage to byte without I / o []
•
Java
Hi, I have a buffered image instance in memory and want to convert it to byte [] to encode it into Base64 string without I / O operation for performance consideration I am using the following APIs:
ByteArrayOutputStream baos = new ByteArrayOutputStream (); ImageIO.write(image,"png",baos); return baos.toByteArray();
However, the API still implicitly writes the image to the OS temporary directory, which will cause a failure when the underlying OS temporary directory is full and the temporary file cannot be created Stack trace:
Caused by: java.io.IOException: No space left on device at java.io.RandomAccessFile.write(RandomAccessFile.java:493) at javax.imageio.stream.FileCacheImageOutputStream.write(FileCacheImageOutputStream.java:134) at javax.imageio.stream.ImageOutputStreamImpl.write(ImageOutputStreamImpl.java:66) at com.sun.imageio.plugins.png.PNGImageWriter.write_magic(PNGImageWriter.java:376) at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:1115) at javax.imageio.ImageWriter.write(ImageWriter.java:628) at javax.imageio.ImageIO.write(ImageIO.java:1480) at javax.imageio.ImageIO.write(ImageIO.java:1554)
Is there an efficient (such as in memory conversion or efficient I / O) way to convert without I / O? Please give me some advice
Solution
Via imageio The setusecache() method disables the imageio cache:
ImageIO.setUseCache(false);
By default, it is based on Javadoc:
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
二维码