JAVA 批处理 调整、压缩png jpg图片大小

时间:2021-07-18 11:36:20

功能性代码,运行在控制台,批处理调整图片

package com.test.main;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
* Eclipse默认把这些受访问限制的API设成了ERROR。
* 只要把Windows-Preferences-Java-Complicer-Errors/Warnings
* 里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。
*/
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


import javax.imageio.ImageIO;

public class ReSizePic {

public static void main(String[] args) {

ReSizePic resize = new ReSizePic();
resize.changeByDir("D:/image", 0, 0, 0.5);
resize.reSizeJpegByproportion("D:/image/1.jpg", "D:/image/2.jpg", 0.5);
}

public void changeByDir(String dirPath, int width, int height){
changeByDir(dirPath, width, height, -1);
}

public void changeByDir(String dirPath, double proportion){
changeByDir(dirPath, 0, 0, proportion);
}

/**
*
* @param dirPath
* @param width
* @param height
* @param proportion
*/
public void changeByDir(String dirPath, int width, int height,
double proportion) {
File dir = new File(dirPath);
if (!dir.isDirectory()) {
System.err.println("is not a dir");
return;
}
File[] fileList = dir.listFiles();
String outputFileDir = dir.getAbsolutePath() + "/out/";
File outputFile = new File(outputFileDir);
if (!outputFile.exists() && !outputFile.isDirectory()) {
outputFile.mkdir();
}
boolean isProportion = false;
if (proportion != -1) {
isProportion = true;
}
for (int i = 0; i < fileList.length; i++) {
File file = fileList[i];
if (file.getName().endsWith(".png")) {
if (isProportion) {
reSizeByproportion(file.getAbsolutePath(), outputFileDir
+ file.getName(), proportion);
} else {

reSizePicture(file.getAbsolutePath(),
outputFileDir + file.getName(), width, height);
}
}else if(file.getName().endsWith("jpg")){
if (isProportion) {
reSizeJpegByproportion(file.getAbsolutePath(),
outputFileDir + file.getName(), proportion);
} else {
reSizeJepgPicture(file.getAbsolutePath(),
outputFileDir + file.getName(), width, height);
}

}
}

}

public void reSizeByproportion(String fromFile, String toFile,
double proportion) {
try {
File fromF = new File(fromFile);

BufferedImage bi = ImageIO.read(fromF);
int newWidth = 0, newHeight = 0;
newWidth = (int) (((double) bi.getWidth()) * proportion);
newHeight = (int) (((double) bi.getHeight()) * proportion);

reSizePicture(fromFile, toFile, newWidth, newHeight);
} catch (Exception e) {
e.printStackTrace();
}

}

public void reSizeJpegByproportion(String fromFile, String toFile,
double proportion) {
try {
File fromF = new File(fromFile);

BufferedImage bi = ImageIO.read(fromF);
int newWidth = 0, newHeight = 0;
newWidth = (int) (((double) bi.getWidth()) * proportion);
newHeight = (int) (((double) bi.getHeight()) * proportion);

reSizeJepgPicture(fromFile, toFile, newWidth, newHeight);
} catch (Exception e) {
e.printStackTrace();
}

}

public void reSizePicture(String fromFile, String toFile, int newWidth,
int newHeight) {
try {
File fromF = new File(fromFile);
BufferedImage bi = ImageIO.read(fromF);
BufferedImage to = new BufferedImage(newWidth, newHeight,
BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = to.createGraphics();
to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,
newHeight, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = to.createGraphics();
Image from = bi.getScaledInstance(newWidth, newHeight,
BufferedImage.SCALE_AREA_AVERAGING);

g2d.drawImage(from, 0, 0, null);
g2d.dispose();

ImageIO.write(to, "png", new File(toFile));

} catch (Exception e) {
e.printStackTrace();
}
}

public void reSizeJepgPicture(String fromFile, String toFile, int newWidth,
int newHeight) {
try {
File fromF = new File(fromFile);
BufferedImage bi = ImageIO.read(fromF);
BufferedImage to = new BufferedImage(newWidth, newHeight,
BufferedImage.TYPE_INT_BGR);
to.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);
FileOutputStream out = new FileOutputStream(toFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(to);
jep.setQuality(0.9f, true);
encoder.encode(to,jep);

out.close();
bi.flush();

} catch (Exception e) {
e.printStackTrace();
}
}

public void reName() {
File filePath = new File("D:/image");

File[] fileList = filePath.listFiles();
for (File file : fileList) {
String fileName = file.getName();
System.out.println("========" + fileName.replace(".png", ""));
String newName = fileName.replace(".png", "");
System.out.println(newName);
if (newName.length() == 1) {
newName = "000" + newName;
} else if (newName.length() == 2) {
newName = "00" + newName;
} else if (newName.length() == 3) {
newName = "0" + newName;
}
boolean bool = file.renameTo(new File("D:/image/" + newName
+ ".png"));
System.err.println(bool);

}

}
}