java 图片质量压缩_java图片高质量压缩

/**

* 根据宽高编辑图片

*

* @param outPath

*            输出文件路径

* @param width

*            输出文件宽

* @param height

*            输出文件高

* @throws Exception

*/

public static void dealImage(String filePath, String outPath, Integer width,

Integer height) throws Exception {

// 读取本地文件:

InputStream is = new FileInputStream(filePath);

//判断图片大小   0---500k 进行4倍压缩       500----1024k   进行6倍压缩     1024以上进行8倍压缩

File picture = new File(filePath);

int cutMultiple = 2;

if (picture.exists()){

//int picsize =Integer.parseInt(new DecimalFormat("0").format(picture.length()/1024.0));//四舍五入

try {

int picsize = (int) (picture.length()/1024.0);//非四舍五入

if(picsize<=512){

cutMultiple=4;

}else if(picsize>512 && picsize<=1024){

cutMultiple=6;

}else if(picsize>1024){

cutMultiple=8;

}

} catch (Exception e) {//假容错处理

cutMultiple=2;

}

}

Image p_w_picpath = ImageIO.read(is);

float tagsize = 200;

int old_w = p_w_picpath.getWidth(null);

int old_h = p_w_picpath.getHeight(null);

int tempsize;

BufferedImage tag = new BufferedImage(old_w/cutMultiple, old_h/cutMultiple, BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(p_w_picpath, 0, 0, old_w/cutMultiple, old_h/cutMultiple, null);

FileOutputStream newp_w_picpath = new FileOutputStream(outPath);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newp_w_picpath);

encoder.encode(tag);

newp_w_picpath.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值