java 图片放大与缩小,java怎么把图片等比例缩小
e = zoomByScale(ratio, img, ext); } ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(newbufferedImage, ext, out); return out.toByteArray(); } //以上都不符合时 强制缩放 Image _img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = image.createGraphics(); graphics.drawImage(_img, 0, 0, null); graphics.dispose(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, ext, out); return out.toByteArray(); } /** *@description: 按比例对图片进行缩放. *@param : scale 缩放比率 *@param : img BufferedImage *@param : ext 图片类型 *@author: YWR *@return: *@throws: *@date: 2020/9/17 0:07 */ public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException { //获取缩放后的长和宽 int _width = (int) (scale * img.getWidth()); int _height = (int) (scale * img.getHeight()); //获取缩放后的Image对象 Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT); //新建一个和Image对象相同大小的画布 BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); //获取画笔 Graphics2D graphics = image.createGraphics(); //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空 graphics.drawImage(_img, 0, 0, null); //释放资源 graphics.dispose(); return image; } /** *@description: 缩放比率计算 *@param : divisor *@param : dividend *@author: YWR *@return: *@throws: *@date: 2020/9/17 0:07 */ private static double CalculateZoomRatio(int divisor, int dividend) { return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue(); }实现方法2
/** *@description: 按比例对图片进行缩放. 检测图片是横图还是竖图 *@param : width 缩放后的宽 *@param : height 缩放后的高 *@param : img BufferedImage *@param : ext 图片类型 如: jpg *@author: YWR *@return: *@throws: *@date: 2020/9/17 0:08 */ public static byte[] zoomByScale(int width, int height, BufferedImage img, String ext) throws IOException { //横向图 if (img.getWidth() >= img.getHeight()) { double ratio = CalculateZoomRatio(width, img.getWidth()); //获取压缩对象 BufferedImage newbufferedImage = zoomByScale(ratio, img, ext); //当图片大于图片压缩高时 再次缩放 if (newbufferedImage.getHeight() > height) { ratio = CalculateZoomRatio(height, newbufferedImage.getHeight()); newbufferedImage = zoomByScale(ratio, img, ext); } ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(newbufferedImage, ext, out); return out.toByteArray(); } //纵向图 if (img.getWidth() < img.getHeight()) { double ratio = CalculateZoomRatio(height, img.getHeight()); //获取压缩对象 BufferedImage newbufferedImage = zoomByScale(ratio, img, ext); //当图片宽大于图片压缩宽时 再次缩放 if (newbufferedImage.getHeight() > height) { ratio = CalculateZoomRatio(width, newbufferedImage.getWidth()); newbufferedImage = zoomByScale(ratio, img, ext); } ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(newbufferedImage, ext, out); return out.toByteArray(); } //以上都不符合时 强制缩放 Image _img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); // 获取缩放比例 double wr=0,hr=0; wr = width * 1.0 / img.getWidth(); hr = height * 1.0 / img.getHeight(); AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null); _img = ato.filter(img, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write((BufferedImage) _img,ext,out);//写入缩减后的图片 return out.toByteArray(); } /** *@description: 按比例对图片进行缩放. *@param : scale 缩放比率 *@param : img BufferedImage *@param : ext 图片类型 *@author: YWR *@return: *@throws: *@date: 2020/9/17 0:07 */ public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException { //获取缩放后的长和宽 int _width = (int) (scale * img.getWidth()); int _height = (int) (scale * img.getHeight()); //设置缩放目标图片模板 Image _img = img.getScaledInstance(_width, _height, Image.SCALE_SMOOTH); //缩放图片 AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null); _img = ato.filter(img, null); return (BufferedImage) _img; } /** *@description: 缩放比率计算 *@param : divisor *@param : dividend *@author: YWR *@return: *@throws: *@date: 2020/9/17 0:07 */ private static double CalculateZoomRatio(int divisor, int dividend) { return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。