100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java图片压缩质量_java图片高质量压缩

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

时间:2020-11-24 18:06:36

相关推荐

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 image = ImageIO.read(is);

float tagsize = 200;

int old_w = image.getWidth(null);

int old_h = image.getHeight(null);

int tempsize;

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

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

FileOutputStream newimage = new FileOutputStream(outPath);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);

encoder.encode(tag);

newimage.close();

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。