BufferedImage 和 byte之间的转换

 转载自:https://www.cnblogs.com/haoqijifeng/p/5567976.html

package com.ac.optimus.wap.util;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageUtil {
/**
* 转换BufferedImage 数据为byte数组
* 
* @param image
* Image对象
* @param format
* image格式字符串.如"gif","png"
* @return byte数组
*/
public static byte[] imageToBytes(BufferedImage bImage, String format) {
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;
}
}

 

你可能感兴趣的:(java)