将BufferedImage转为byte[]数组,亲测可用
有时候我们仅仅想根据BufferedImage来获得一个byte[],以便在接下来对其进行base64编码。这时可以考虑使用如下的方法,可以避免生成图片到磁盘,再从磁盘读取转化为byte[]再进行编码。
/**
* 将BufferedImage转换为byte[]
* @param image
* @return
*/
public byte[] bufferedImageToByteArray(BufferedImage image) throws IOException{
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
return os.toByteArray();
}
本文介绍了一种直接将BufferedImage转换为byte[]数组的实用方法,避免了文件I/O操作,适用于编码前的数据预处理,提高效率。

3444

被折叠的 条评论
为什么被折叠?



