public static byte [] scaleImage(byte [] photo) {
try {
ByteArrayInputStream in = new ByteArrayInputStream(photo); //将b作为输入流;
BufferedImage image = ImageIO.read(in); //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
int imageWidth = image.getWidth();
double scale = (double) 600 / imageWidth;
int imageHeight = image.getHeight();
int height = (int) (imageHeight * scale);
int width = (int) (imageWidth * scale);
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale);
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage image1 = bilinearScaleOp.filter(image, new BufferedImage(width, height, image.getType()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(image1, "jpg", out);
if(!flag){
logger.info("压缩上传大数据抓拍照片失败");
}
photo = out.toByteArray();
} catch (Exception e) {
logger.error("图片压缩出错:", e);
return photo;
}
return photo;
}
借鉴:https://blog.youkuaiyun.com/wfs31415926/article/details/79392357/