java压缩图片大小_java压缩图片、等比例压缩图片

java提供了图片处理支持,而且使用很简单,功能也比较强大(如支持设置压缩后图片质量),直接看代码即可。

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

/**

* Created by halfsking on 2016/3/29.

*/

public class ImageCompressUtil {

// private static final ILog LOG = LogFactory.getLog(ImageCompressUtil.class);

public static byte[] compressImage(byte[] data)

{

return compressImage(data,600,400,0.85f);

}

public static byte[] compressImage(byte[] data,int width,int height)

{

return compressImage(data,width,height,0.85f);

}

public static byte[] compressImage(byte[] data,int width,int height,float quality)

{

width = width>600?600:width;//限定宽度为600

height = height>400?400:height;//限定高度为400

quality = quality<0.8f?0.8f:quality;

int maxLength = 71*1024;//70K以下不压缩

if(data==null || data.length

{

return data;

}

try {

InputStream inputStream = new ByteArrayInputStream(data);

Image oriImg = ImageIO.read(inputStream);

int ori_width = oriImg.getWidth(null);

int ori_height = oriImg.getHeight(null);

if(width>ori_width || height>ori_height)

{

return data;

}

float ori_rate = (float)ori_width / (float)ori_height;

float rate = (float)width / (float)height;

if (ori_rate > rate) {

return compressByWidth(ori_width, ori_height, width, oriImg, quality);

} else {

return compressByHeight(ori_width, ori_height, height, oriImg, quality);

}

} catch (IOException e) {

LOG.error("ImageCompressUtil compressImage IOException failed,",e);

}

LOG.warn("ImageCompressUtil compressImage failed,now return the origin data.");

return data;

}

/**

* 以宽度为基准,等比例放缩图片

* @param w int 新宽度

*/

private static byte[] compressByWidth(int ori_width,int ori_height,int w,Image oriImg,float quality) throws IOException {

int h = (int) (ori_height * w / ori_width);

return compress(w, h, oriImg, quality);

}

/**

* 以高度为基准,等比例缩放图片

* @param h int 新高度

*/

private static byte[] compressByHeight(int ori_width,int ori_height,int h,Image oriImg,float quality) throws IOException {

int w = (int) (ori_width * h / ori_height);

return compress(w, h, oriImg, quality);

}

/**

* 压缩/放大图片到固定的大小

* @param w int 新宽度

* @param h int 新高度

*/

private static byte[] compress(int w, int h,Image oriImg,float quality) throws IOException {

BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );

// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

//image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图

image.getGraphics().drawImage(oriImg.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0,0,null);

ByteArrayOutputStream out = new ByteArrayOutputStream();

// 可以正常实现bmp、png、gif转jpg

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(image);

/* 压缩质量 */

jep.setQuality(quality, true);

encoder.encode(image, jep);// JPEG编码

return out.toByteArray();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值