java实现图片比率缩放-mile米乐体育
本文实例为大家分享了java实现图片比率缩放的具体代码,供大家参考,具体内容如下
通过thumbnails实现图片缩放
需要导入pom依赖,可以到中央仓库获取最小的工具包
//读取图片 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实现图片缩放
调用方法
initimage("自定义压缩图", zoombysize(750,1334,bufferedimage,"png"));//调用方法
具体方法实现1
/** *@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
/** *@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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。