ImageIO的图片压缩算法

调用CompressPictureUtils.compress(要压缩的图片路径,保存的图片路径)可以对图片进行压缩。

左边的原图,右边是压缩后的图片。

调用函数的方法:

	public static void main(String[] args) {
		CompressPictureUtils.compress("D:\\1.jpg", "D:\\0.jpg");
	}

可以设置压缩属性:


	/**
	 * 图片压缩最大宽1920像素
	 */
	public static final Integer PICTURE_COMPRESS_WIDTH_MAX = 1920;// 1920像素
	/**
	 * 图片压缩最大高1920像素
	 */
	public static final Integer PICTURE_COMPRESS_HEIGHT_MAX = 1920;// 1920像素
	/**
	 * 图片压缩最小容量64KB
	 */
	public static final Integer PICTURE_COMPRESS_MIN = 1 << 16;// 64KB
	/**
	 * 图片压缩最大容量64MB
	 */
	public static final Integer PICTURE_COMPRESS_MAX = 1 << 26;// 64MB
	/**
	 * 图片压缩最小容量差64KB
	 */
	public static final Integer PICTURE_COMPRESS_DIFFER_MIN = 1 << 16;// 64KB

工具类:(复制后可以直接使用)

package util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;

import javax.imageio.ImageIO;

/**
 * 图片压缩算法
 * 
 * @author ALI
 *
 */
public class CompressPictureUtils {

	public static void main(String[] args) {
		CompressPictureUtils.compress("D:\\1.jpg", "D:\\0.jpg");
	}

	/**
	 * 图片压缩最大宽1920像素
	 */
	public static final Integer PICTURE_COMPRESS_WIDTH_MAX = 1920;// 1920像素
	/**
	 * 图片压缩最大高1920像素
	 */
	public static final Integer PICTURE_COMPRESS_HEIGHT_MAX = 1920;// 1920像素
	/**
	 * 图片压缩最小容量64KB
	 */
	public static final Integer PICTURE_COMPRESS_MIN = 1 << 16;// 64KB
	/**
	 * 图片压缩最大容量64MB
	 */
	public static final Integer PICTURE_COMPRESS_MAX = 1 << 26;// 64MB
	/**
	 * 图片压缩最小容量差64KB
	 */
	public static final Integer PICTURE_COMPRESS_DIFFER_MIN = 1 << 16;// 64KB

	public static void compress(String sourceImagePath, String destinationImagePath) {
		try {
			/* 过滤小于64kb和大于64mb的图片 */
			File sourceImageFile = new File(sourceImagePath);
			File destinationImageFile = new File(destinationImagePath);
			long sourceImageSize = sourceImageFile.length();
			if (sourceImageSize == 0) {
				throw new Exception("empty");
			}
			if (sourceImageSize < PICTURE_COMPRESS_MIN) {
				throw new Exception("small");
			}
			if (sourceImageSize > PICTURE_COMPRESS_MAX) {
				throw new Exception("large");
			}
			/* 设置图片大小 */
			BufferedImage inputImage = ImageIO.read(sourceImageFile);
			int sourceImageWidth = inputImage.getWidth();
			int sourceImageHeight = inputImage.getHeight();
			int destinationImageWidth = sourceImageWidth;
			int destinationImageHeight = sourceImageHeight;
			if (sourceImageWidth > PICTURE_COMPRESS_WIDTH_MAX || sourceImageHeight > PICTURE_COMPRESS_HEIGHT_MAX) {
				if (sourceImageWidth > sourceImageHeight) {
					destinationImageWidth = PICTURE_COMPRESS_WIDTH_MAX;
					destinationImageHeight = destinationImageWidth * sourceImageHeight / sourceImageWidth;
				} else {
					destinationImageHeight = PICTURE_COMPRESS_HEIGHT_MAX;
					destinationImageWidth = destinationImageHeight * sourceImageWidth / sourceImageHeight;
				}
			}
			/* 压缩图片 */
			Image image = inputImage.getScaledInstance(destinationImageWidth, destinationImageHeight,
					Image.SCALE_DEFAULT);
			BufferedImage outputImage = new BufferedImage(destinationImageWidth, destinationImageHeight,
					BufferedImage.TYPE_INT_RGB);
			Graphics graphics = outputImage.getGraphics();
			graphics.drawImage(image, 0, 0, null);
			graphics.dispose();
			ImageIO.write(outputImage, "jpg", destinationImageFile);
			/* 过滤比原图小8kb以下的的图片 */
			long destinationImageSize = destinationImageFile.length();
			if (destinationImageSize > (sourceImageSize - PICTURE_COMPRESS_DIFFER_MIN)) {
				throw new Exception("low");
			}
		} catch (Exception e) {
			try {
				String msg = e.getMessage();
				if (msg == "small" || msg == "low") {// 过小或压缩率过低,保存原图
					File sourceImageFile = new File(sourceImagePath);
					File destinationImageFile = new File(destinationImagePath);
					destinationImageFile.delete();
					Files.copy(sourceImageFile.toPath(), destinationImageFile.toPath());
				} else {// 空或过大或异常,保存空图
					File emptyfile = new File(destinationImagePath);
					emptyfile.delete();
					emptyfile.createNewFile();
				}
			} catch (Exception e1) {
			}
		}
	}
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值