[b]1. 从byte[]得到Image[/b]
[b]2. 从Image得到byte[][/b]
private static Image createImage(byte[] imageBytes) {
Image image = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
image = new Image(null, bais);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}[b]2. 从Image得到byte[][/b]
public static byte[] getImageBytes(Image image) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(baos, image.type);
byte[] imageByteArray = baos.toByteArray();
try {
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return imageByteArray;
}
本文介绍如何在Java中实现图片与字节流之间的相互转换,包括使用byte[]获取Image对象及从Image对象中提取byte[]的方法。

187

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



