JAVA GUI 中如何将一张图片读到一个byte数组或其他数组中,有如何将该数组再转换成图片?
推荐于2016-03-29
展开全部
/**
* 转换Image数据为byte数组
* @param image
* Image对象
* @param format
* image格式字符串.如"gif","png"
* @return byte数组
*/
public static byte[] imageToBytes(Image image, String format) {
BufferedImage bImage = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, format, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* 转换byte数组为Image
* @param bytes
* @return Image
*/
public static Image bytesToImage(byte[] bytes) {
Image image = Toolkit.getDefaultToolkit().createImage(bytes);
try {
MediaTracker mt = new MediaTracker(new Label());
mt.addImage(image, 0);
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
return image;
}
* 转换Image数据为byte数组
* @param image
* Image对象
* @param format
* image格式字符串.如"gif","png"
* @return byte数组
*/
public static byte[] imageToBytes(Image image, String format) {
BufferedImage bImage = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, format, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* 转换byte数组为Image
* @param bytes
* @return Image
*/
public static Image bytesToImage(byte[] bytes) {
Image image = Toolkit.getDefaultToolkit().createImage(bytes);
try {
MediaTracker mt = new MediaTracker(new Label());
mt.addImage(image, 0);
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
return image;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询