package com.caac.knsgl.utils; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /******************************************************************************* * 图片压缩类 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法 * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) */ public class ImgCompressUtil { private File file = null; // 文件对象 private String inputDir; // 输入图路径 private String outputDir; // 输出图路径 private String inputFileName; // 输入图文件名 private String outputFileName; // 输出图文件名 private int outputWidth = 100; // 默认输出图片宽 private int outputHeight = 100; // 默认输出图片高 private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放) public ImgCompressUtil() { // 初始化变量 inputDir = ""; outputDir = ""; inputFileName = ""; outputFileName = ""; outputWidth = 100; outputHeight = 100; } public void setInputDir(String inputDir) { this.inputDir = inputDir; } public void setOutputDir(String outputDir) { this.outputDir = outputDir; } public void setInputFileName(String inputFileName) { this.inputFileName = inputFileName; } public void setOutputFileName(String outputFileName) { this.outputFileName = outputFileName; } public void setOutputWidth(int outputWidth) { this.outputWidth = outputWidth; } public void setOutputHeight(int outputHeight) { this.outputHeight = outputHeight; } public void setWidthAndHeight(int width, int height) { this.outputWidth = width; this.outputHeight = height; } /** * 获得图片大小 传入参数 String path :图片路径 */ public long getPicSize(String path) { file = new File(path); return file.length(); } /** * 图片处理 * * @return */ public String compressPic() { try { // 获得源文件 file = new File(inputDir + inputFileName); if (!file.exists()) { return ""; } Image img = ImageIO.read(file); // 判断图片格式是否正确 if (img.getWidth(null) == -1) { System.out.println(" can't read,retry!" + "<BR>"); return "no"; } else { int newWidth; int newHeight; // 判断是否是等比缩放 if (this.proportion == true) { // 为等比缩放计算输出的图片宽度及高度 double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1; double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1; // 根据缩放比率大的进行缩放控制 double rate = rate1 > rate2 ? rate1 : rate2; newWidth = (int) (((double) img.getWidth(null)) / rate); newHeight = (int) (((double) img.getHeight(null)) / rate); } else { newWidth = outputWidth; // 输出的图片宽度 newHeight = outputHeight; // 输出的图片高度 } BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); /* * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 */ tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(outputDir + outputFileName); // JPEGImageEncoder可适用于其他图片类型的转换 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } catch (IOException ex) { ex.printStackTrace(); } return "ok"; } /** * 初始化参数 * * @param inputDir * 输入图路径 * @param outputDir * 输出图路径 * @param inputFileName * 输入图文件名 * @param outputFileName * 输出图文件名 * @return */ public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName) { // 输入图路径 this.inputDir = inputDir; // 输出图路径 this.outputDir = outputDir; // 输入图文件名 this.inputFileName = inputFileName; // 输出图文件名 this.outputFileName = outputFileName; return compressPic(); } /** * 按比例压缩图片 * * @param inputDir * 输入图路径 * @param outputDir * 输出图路径 * @param inputFileName * 输入图文件名 * @param outputFileName * 输出图文件名 * @param width * 压缩后的宽度 * @param height * 压缩后的高度 * @param gp * 是否等比缩放 * @return */ public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) { // 输入图路径 this.inputDir = inputDir; // 输出图路径 this.outputDir = outputDir; // 输入图文件名 this.inputFileName = inputFileName; // 输出图文件名 this.outputFileName = outputFileName; // 设置图片长宽 setWidthAndHeight(width, height); // 是否是等比缩放 标记 this.proportion = gp; return compressPic(); } /** * 按图片大小(KB单位)循环压缩图片 * * @param oldImgPath * @param oldImgName * @param newImgPath * @param newImgName * @param size * KB单位 * @throws IOException */ public static void cycleImg(String oldImgPath, String oldImgName, String newImgPath, String newImgName, long size) throws IOException { ImgCompressUtil mypic = new ImgCompressUtil(); sameSizeImg(oldImgPath,oldImgName,newImgPath,newImgName); long newSize = mypic.getPicSize(newImgPath + newImgName) / 1024; if (newSize > size) { cycleImg(newImgPath, newImgName, newImgPath, newImgName, size); } } /** * 按图片原高宽压缩图片 * * @param oldImgPath * @param oldImgName * @param newImgPath * @param newImgName * @param size * KB单位 * @throws IOException */ public static void sameSizeImg(String oldImgPath, String oldImgName, String newImgPath, String newImgName) throws IOException { ImgCompressUtil mypic = new ImgCompressUtil(); File srcFile = new java.io.File(oldImgPath + oldImgName); Image src = ImageIO.read(srcFile); int srcWidth = src.getWidth(null); int srcHeight = src.getHeight(null); mypic.compressPic(oldImgPath, newImgPath, oldImgName, newImgName, srcWidth, srcHeight, true); } // main测试 // compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) public static void main(String[] arg) throws IOException { ImgCompressUtil mypic = new ImgCompressUtil(); String oldImgPath = "E:\\工作需求\\贫困生申请表\\sourceImg\\"; String oldImgName = "P60906-104527.jpg"; String newImgPath = "E:\\工作需求\\贫困生申请表\\targeImg\\"; String newImgName = "test.jpg"; long size = 500; long oldSize = mypic.getPicSize(oldImgPath + oldImgName) / 1024; System.out.println("输入的图片大小:" + oldSize + "KB"); int start = (int)System.currentTimeMillis(); //开始时间 //cycleImg(oldImgPath, oldImgName, newImgPath, newImgName, size); sameSizeImg(oldImgPath, oldImgName, newImgPath, newImgName); int end = (int)System.currentTimeMillis(); //结束时间 System.out.println("总共用了:"+(end-start)+" 毫秒"); long newSize = mypic.getPicSize(newImgPath + newImgName) / 1024; System.out.println("输出的图片大小:" + newSize + "KB"); } }
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.regex.Pattern; /** * 读取文件夹下的所有附件,并把图片类型压缩公共类 */ public class ReadFileCompressUtil { public ReadFileCompressUtil() { super(); } /** * 读取某个文件夹下的所有文件,并把图片类型压缩 * @param filepath 读取的文件夹路径 * @param targetSize 需要把图片压缩到 targetSize KB单位内 * @return * @throws FileNotFoundException * @throws IOException */ public static boolean readfile(String filepath, long targetSize) throws FileNotFoundException, IOException { String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$"; //图片类型 正则表达式 Pattern pattern = Pattern.compile(reg); try { File file = new File(filepath); if (!file.isDirectory()) { //文件 } else if (file.isDirectory()) { //文件夹 String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) { String sourcePath = filepath + "\\"; String sourceName = readfile.getName(); long size = readfile.length() / 1024; //KB if (targetSize >0 && pattern.matcher(sourcePath + sourceName).find() && size > targetSize) { //图片类型,并且大于所需的格式大小,则压缩 new ImgCompressUtil().cycleImg(sourcePath, sourceName, sourcePath, sourceName, targetSize); }else{ //不指定压缩大小 new ImgCompressUtil().sameSizeImg(sourcePath, sourceName, sourcePath, sourceName); } } else if (readfile.isDirectory()) { readfile(filepath + "\\" + filelist[i], targetSize); } } } } catch (FileNotFoundException e) { System.out.println("readfile() Exception:" + e.getMessage()); } return true; } public static void main(String[] args) { try { //1,文件夹路径,压缩图片,指定大小(KB) // readfile("E:\\工作需求\\贫困生申请表\\targeImg", 100); //2,文件夹路径,压缩图片,不指定大小,第二个参数小于0即可 readfile("E:\\工作需求\\贫困生申请表\\targeImg", -1); } catch (FileNotFoundException ex) { } catch (IOException ex) { } System.out.println("ok"); } }
--摘自 困难生申请的公共类