我需要将 a 转换BufferedImage为 a byte[],但它太慢了。该字节最终被 base64 编码并发送到 android 客户端。我一直使用的方法是这样的:
public static byte[] ImageToBytes(BufferedImage im) throws IOException
{
//make sure its NN
if(im!=null)
{
//create a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//write our image to it
ImageIO.write( im, "png", baos );
//flush the stream
baos.flush();
//get the image in byte form
byte[] imageInByte = baos.toByteArray();
//close the stream
baos.close();
//return our value encoded in base64
return imageInByte;
}
return null;
}
这对我的程序来说太慢了。更改png为jpeg使其在移动端失败。该JpegCodec版本在移动端也失败了。失败是指 Android 方法BitmapFactory.decodeByteArray()返回null.