java 图片压缩工具类


public class Demo {

public static void main(String[] args) throws Exception {

//文件夹遍历图片,文件夹地址自定。

File fileFolder=new File("D:/IMG");

File[] fileList=fileFolder.listFiles();

for(File file:fileList){

InputStream input = new FileInputStream(file);

byte[] data = new byte[(input.available())];

input.read(data);

input.close();

String type="jpg";

data = zipImageToScaledSize(data,600, 800, type,100000);

OutputStream output = new FileOutputStream("D:/IMG/"+Math.random()+"."+type);

output.write(data);

output.close();

}

}

/**

*

* 压缩图片,并等比缩小。

*

* @author aren

* @param data

* 输入图片数据的byte[]。

* @param width

* 最大输出宽度,但是最后会根据图片本身比例调整。推荐值800。

* @param height

* 最大输出高度,但是最后会根据图片本身比例调整。推荐值600。

* @param type

* 指定最后存储的图片类型,支持字符串jpg,png,gif,bmp,jpeg。如果为null,则默认输出jpg格式图片。

* @param maxSize

* 指定最大输出图片的容量大小。可以为null表示不指定压缩容量大小。不要小于10000,推荐100000。

* @return 输出图片数据的byte[]。

* @throws Exception

*/

public static byte[] zipImageToScaledSize(byte[] data, int width, int height, String type, Integer maxSize)

throws Exception {

if (data == null) {

return null;

}

if (width <= 0 || height <= 0) {

width = 800;

height = 600;

}

// 设定输出格式

String[] supportType = new String[] { "jpg", "png", "bmp", "jpeg", "gif" };

if (type == null || !ArrayUtils.contains(supportType, type)) {

type = "jpg";

}

int pointedHeight;

int pointedWidth;

ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

BufferedImage bufferedImage = ImageIO.read(inputStream);

inputStream.close();

int originalHeight = bufferedImage.getHeight();

int originalWidth = bufferedImage.getWidth();

// 设定等比例压缩。

if ((originalHeight / (double) height) > (originalWidth / (double) width)) {

pointedHeight = NumberUtils.min(height, originalHeight);

pointedWidth = -1;

} else {

pointedHeight = -1;

pointedWidth = NumberUtils.min(width, originalWidth);

}

// 压缩图片,此处附上颜色类型BufferedImage.TYPE_INT_RGB。Color.WHITE,可以有效避免png转jpg时图片发红的问题。

Image newImage = bufferedImage.getScaledInstance(pointedWidth, pointedHeight, Image.SCALE_SMOOTH);

BufferedImage newBufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),

BufferedImage.TYPE_INT_RGB);

newBufferedImage.getGraphics().drawImage(newImage, 0, 0,Color.WHITE, null);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

ImageIO.write(newBufferedImage, type, byteArrayOutputStream);

byteArrayOutputStream.close();

data = byteArrayOutputStream.toByteArray();

if (maxSize != null && data.length > maxSize) {

// 设定递归的保险,以免图片质量太差

if (maxSize < 5000 && (data.length > 10 * maxSize)) {

maxSize = 5000;

}

// 递归压缩

double scale = Math.max(Math.pow(maxSize / (double) data.length, 0.5), 0.9);

return zipImageToScaledSize(data, (int) (width * scale), (int) (height * scale), type, maxSize);

} else {

return data;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值