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;
}