Java图片等比缩放,通过jdk自带的Graphics
实现对图片的等比缩放
//重置大小(太宽没啥用)最大支持1.5倍图(1150*1.5)
int maxWidth=(int)(920*1.5);
//大小重置
//图片大小
BufferedImage read = ImageIO.read(new ByteArrayInputStream(imageFileBytes));
int width = read.getWidth();
int height = read.getHeight();
if (width>maxWidth){
int dHeight = new BigDecimal(height).divide(new BigDecimal(width).divide(new BigDecimal(maxWidth), 2, RoundingMode.HALF_UP), 2, RoundingMode.HALF_UP).intValue();
BufferedImage resizeImage=new BufferedImage(maxWidth,dHeight,BufferedImage.TYPE_INT_RGB);
Graphics graphics = resizeImage.getGraphics();
graphics.drawImage(read,0,0, maxWidth,dHeight,null);
graphics.dispose();
//重置大小
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ImageIO.write(resizeImage,format.substring(1),bos);
imageFileBytes=bos.toByteArray();
}
over
http://blog.xqlee.com/article/2504190931463051.html