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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中有多种方式可以实现高质量图片压缩,以下是其中一些常用的方法: 1. 使用Java自带的ImageIO类进行压缩: ```java File imageFile = new File("image.jpg"); BufferedImage originalImage = ImageIO.read(imageFile); // 压缩后的图片尺寸 int newWidth = originalImage.getWidth() / 2; int newHeight = originalImage.getHeight() / 2; // 创建一个新的缩放后的图像 BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); // 使用Graphics2D对象对图像进行缩放 Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g.dispose(); // 将压缩后的图像写入文件 ImageIO.write(resizedImage, "jpg", new File("compressed_image.jpg")); ``` 2. 使用Java的第三方库ImageJ进行压缩: ```java File imageFile = new File("image.jpg"); ImagePlus imp = new ImagePlus(imageFile.getAbsolutePath()); // 压缩后的图片尺寸 int newWidth = imp.getWidth() / 2; int newHeight = imp.getHeight() / 2; // 缩放图像 imp.setProcessor(imp.getProcessor().resize(newWidth, newHeight)); // 将压缩后的图像写入文件 FileSaver fs = new FileSaver(imp); fs.saveAsJpeg("compressed_image.jpg"); ``` 3. 使用Java的第三方库ImgScalr进行压缩: ```java File imageFile = new File("image.jpg"); BufferedImage originalImage = ImageIO.read(imageFile); // 压缩后的图片尺寸 int newWidth = originalImage.getWidth() / 2; int newHeight = originalImage.getHeight() / 2; // 缩放图像 BufferedImage resizedImage = Scalr.resize(originalImage, newWidth, newHeight); // 将压缩后的图像写入文件 ImageIO.write(resizedImage, "jpg", new File("compressed_image.jpg")); ``` 以上这些方法都可以实现高质量图片压缩,具体使用哪种方法可以根据实际情况选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值