您的位置:首页 > 其它

记录几个有关image处理的API

2016-02-17 10:39 363 查看
1 将字节数组转换成BufferedImage对象

public static BufferedImage byte2BufferedImage(byte[] imageByte) throws Exception{
ByteArrayInputStream in  = new ByteArrayInputStream(imageByte);
BufferedImage image = ImageIO.read(in);
return image;
}


2 将BufferedImage对象转换成字节数组

public static byte[] bufferedImage2Byte(BufferedImage bufferImage) throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(bufferImage, "png", out);
byte[] b = out.toByteArray();
return b;
}


3 通过BufferedImage设置图片大小

public static BufferedImage zoomOutImage(BufferedImage originalImage, int width, int height){
BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return newImage;
}


4 将字符串转换为base64数组

public static byte[] getBase64Bytes(String image){
BASE64Decoder decoder = new BASE64Decoder();
try{
byte[] decodedBytes = decoder.decodeBuffer(image);
return decodedBytes;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  image常用API