本文实例为大家分享了Java实现图片比率缩放的具体代码,供大家参考,具体内容如下
通过Thumbnails实现图片缩放
需要导入pom依赖,可以到中央仓库获取最小的工具包
1 2 3 4 5 |
< dependency > < groupId >net.coobird</ groupId > < artifactId >thumbnailator</ artifactId > < version >0.4.8</ version > </ dependency > |
1 2 3 4 5 |
//读取图片 BufferedImage bufferedImage = ImageIO.read( new ByteArrayInputStream(out.toByteArray())); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); Thumbnails.of(bufferedImage).size( 750 , 1344 ).outputFormat( "png" ).toOutputStream(out2); //缩放图片 InitImage( "缩放图" , out2.toByteArray()); //显示图片 |
java API实现图片缩放
调用方法
1 |
InitImage( "自定义压缩图" , zoomBySize( 750 , 1334 ,bufferedImage, "png" )); //调用方法 |
具体方法实现1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
/** *@description: 按比例对图片进行缩放. 检测图片是横图还是竖图 *@param : width 缩放后的宽 *@param : height 缩放后的高 *@param : img BufferedImage *@param : ext 图片类型 如: jpg *@author: YWR *@return: *@throws: *@date: 2020/9/17 0:08 */ public static byte [] zoomBySize( 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_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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
/** *@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(); } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://laiwei.blog.csdn.net/article/details/108633119