import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.Files;
import java.util.List;
import javax.imageio.ImageIO;
public class ImgCompressUtil {
static BufferedImage img = null;
//static List<String> list = null;
private static final Logger logger = LoggerFactory.getLogger(ImgCompressUtil.class);
/**
* 构造函数
*/
public ImgCompressUtil(String fileName) throws IOException {
File file = new File(fileName);// 读入文件
img = ImageIO.read(file); // 构造Image对象
}
/**
* 图片转换
* @param newUrl
* @param sourceUrl
* @throws IOException
*/
public static void ImageCompression(String newUrl, String sourceUrl) throws IOException {
System.out.println("开始压缩");
operationImg(newUrl,sourceUrl);
System.out.println("压缩完成");
}
/**
*图片操作
* @param newUrl
* @param sourceUrl
* @throws IOException
*/
public static void operationImg(String newUrl , String sourceUrl) throws IOException {
File folder = new File(sourceUrl);
String url = null;
File[] files = folder.listFiles();
for (File file : files) {
try {
if (file.isFile()) {
url = sourceUrl + '\\' + file.getName();
String newsUrl = newUrl ;
String allNewsUrl = newsUrl + '\\' + file.getName();
// System.out.println("源地址" + url);
// System.out.println("新地址" + newsUrl);
// if (list.contains(file.getName())){
ImgInformation information = ImgCompressUtil.getImgInformation(url);
File fileMkdir = new File(newsUrl);
fileMkdir.mkdirs();
if (Float.parseFloat(information.getImgSize()) > 3000){
ImgCompressUtil imgCom = new ImgCompressUtil(url);
imgCom.resize(information.getImgWidth(), information.getImgHeight(),allNewsUrl);
}else {
File fileurl = new File(url);
File allNewsUrlFile = new File(allNewsUrl);
Files.copy(fileurl.toPath(), allNewsUrlFile.toPath());
}
// }
}else{
String imgUrl = sourceUrl + '\\' + file.getName();
String newsUrl = newUrl + '\\' + file.getName();
operationImg(newsUrl,imgUrl);
}
} catch (Exception e) {
// System.err.println("问题图片:" + url + "报错信息:" + e);
logger.info("问题图片:" + url + "报错信息:" + e);
}
continue;
}
}
/**
* 获取图片信息
* @param url
* @return
* @throws IOException
*/
public static ImgInformation getImgInformation(String url) throws IOException {
File picture = new File(url);
BufferedImage sourceImg = ImageIO.read(picture);
ImgInformation information =new ImgInformation();
// 源图大小
information.setImgSize(String.format("%.1f",picture.length()/1024.0));
// 源图宽度
information.setImgWidth(sourceImg.getWidth());
// 源图高度
information.setImgHeight(sourceImg.getHeight());
return information;
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w int 新宽度
* @param h int 新高度
* allNewsUrl 目标路径 + 图片名
*/
public static void resize(int w, int h, String allNewsUrl) throws IOException {
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
File destFile = new File(allNewsUrl);
// destFile.mkdirs();
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image); // JPEG编码
out.close();
}
}
class ImgInformation{
private String imgSize;
private int imgWidth;
private int imgHeight;
public String getImgSize() {
return imgSize;
}
public void setImgSize(String imgSize) {
this.imgSize = imgSize;
}
public int getImgWidth() {
return imgWidth;
}
public void setImgWidth(int imgWidth) {
this.imgWidth = imgWidth;
}
public int getImgHeight() {
return imgHeight;
}
public void setImgHeight(int imgHeight) {
this.imgHeight = imgHeight;
}
}