/**
* 转换Image数据为byte数组
*
* @param image Image对象
* @param format image格式字符串.如 "jpeg","png"
* @return byte数组
*/
public static byte[]imageToBytes(Image image,String format)
{
BufferedImage bImage= new BufferedImage(image.getWidth(null),
image.getHeight(null),BufferedImage.TYPE_INT_RGB); //此处加粗部分,是设置图片背景颜色,
原英文解释:Create a buffered image using the default color model
之前是TYPE_INT_ARGB,图片显示出来会有一层粉红色背景,改为TYPE_INT_RGB后,就是白色正常的了
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();
}