将缓冲图像转换为不带 I/O 的字节 []
2022-09-04 19:22:42
嗨,我在内存中有一个BufferedImage实例,并希望将其转换为byte[]以编码为base64字符串,而无需I / O操作以考虑性能。我使用以下 API:
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ImageIO.write(image,"png",baos);
return baos.toByteArray();
但是,此 API 仍会将映像隐式写入 OS 临时目录,如果基础操作系统临时目录已满且无法创建临时文件,这将导致失败。堆栈跟踪:
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)
有没有一种有效的(如内存中转换或高效 I/O)方法可以在没有 I/O 的情况下进行转换?请指教。