本文的有损压缩算法,是每隔一行或者一列,就删去一行或一列的像素。如下图所示,下图中红线表示要删除的行和列的元素。
下面直接放代码:
常量类 Constant.java
package zhangchao.common;
/**
*
* 常量类
* @author 张超
*
*/
public class Constant {
public static final String INPUT_DIR_PATH = "D:\\yourzcinputdir\\";
public static final String OUTPUT_DIR_PATH = "D:\\yourzcoutputdir\\";
}
Main.java
package zhangchao;
import zhangchao.common.Constant;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 含有 main 方法
* @author zhangchao
*
*/
public class Main {
/**
* 有损压缩图片
* @param file 原文件
* @param outputFile 输出的压缩后的文件
* @return 压缩后的文件
*/
public static File one(final File file, final File outputFile, final String formatName) {
try {
// 原图片
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
// 压缩后图片的宽度
int widthNew;
if (width % 2 == 0) {
widthNew = width / 2;
} else {
widthNew = width / 2 + 1;
}
// 压缩后图片的高度
int heightNew;
if (height % 2 == 0) {
heightNew = height / 2;
} else {
heightNew = height / 2 + 1;
}
// 压缩后的图片
BufferedImage imageNew = new BufferedImage(widthNew, heightNew, image.getType());
// 从0开始计算行数和列数。把同时位于偶数行和偶数列的元素复制到新的压缩后的图片中。
for (int x = 0; x < width; x+=2) {
for (int y = 0; y < height; y+=2) {
int rgb = image.getRGB(x, y);
int x1 = x/2;
int y1 = y/2;
imageNew.setRGB(x1, y1, rgb);
}
}
ImageIO.write(imageNew, formatName, outputFile);
return outputFile;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
File dir = new File(Constant.INPUT_DIR_PATH);
File[] files = dir.listFiles();
for (File file : files) {
// 如果图片小于200K就不压缩
if (file.length() < 200 * 1024) {
try {
Files.copy(Paths.get(file.getAbsolutePath()), Paths.get(Constant.OUTPUT_DIR_PATH + file.getName()));
} catch (IOException e) {
e.printStackTrace();
}
continue;
}
File result = file;
while(null != result && result.length() >= 200 * 1024) {
result = one(result, new File( Constant.OUTPUT_DIR_PATH+ file.getName()), "jpg");
}
}
long t2 = System.currentTimeMillis();
System.out.println("time=" + (t2 - t1));
}
}
// 14654
// 10909
// 张
Main类的作用:循环遍历输入目录中的每个文件,如果小于200K就不压缩,直接复制到输出目录;如果大于等于200K,就进行反复压缩,直到低于200K为止。上面代码中one方法每次压缩,图片体积大约会变成原来的25%