实现三个功能:一是实现图片等比例压缩和精度压缩;二是实现批量读取目录下包含子目录所有图片;三是实现批量压缩图片。
Linux中压缩文件直接修改目录地址即可,注意如果是windows系统直接切换目录格式,windows是“\\”,linux是“/”。
代码如下所示:
package org.common.util; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; public class ImageZipUtil { /** * 用于压缩所有图片的功能 * @param args * @throws FileNotFoundException * @throws IOException */ public static void main(String[] args) throws FileNotFoundException, IOException { Map<String,String> map = new HashMap<String,String>(); readfile("/mnt/app/project/files/upload/images", map); for (Map.Entry<String,String> entry : map.entrySet()) { System.out.println("图片名称为" + entry.getValue() + "正在处理..."); zipImageFile(new File(entry.getValue()), new File(entry.getValue()), 1200, 0, 1f); } } public static boolean readfile(String filepath, Map<String,String> map) throws FileNotFoundException, IOException { try { File file = new File(filepath); if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "/" + filelist[i]); System.out.println("------------------>" + filepath + "/" + filelist[i]); if (!readfile.isDirectory()) { if (readfile.getName().contains("jpg") || readfile.getName().contains("png")) { map.put(readfile.getName(),readfile.getAbsolutePath()); } } else if (readfile.isDirectory()) { System.out.println("------------------>文件目录。。。。。。。。。"); readfile(filepath + "/" + filelist[i], map); } } } } catch (FileNotFoundException e) { System.out.println("readfile() Exception:" + e.getMessage()); } return true; } /** * 根据设置的宽高等比例压缩图片文件<br> * 先保存原文件,再压缩、上传 * * @param oldFile 要进行压缩的文件 * @param newFile 新文件 * @param width 宽度 * @param height 高度 * @param quality 质量 * @return 返回压缩后的文件的全路径 */ public static String zipImageFile(File oldFile, File newFile, int width, int height, float quality) { if (oldFile == null) { return null; } if ((oldFile.length() / 1024) < 800) { System.out.println(oldFile.getName() + "文件小于800k"); return null; } try { /** 对服务器上的临时文件进行处理 */ Image srcFile = ImageIO.read(oldFile); int w = srcFile.getWidth(null); int h = srcFile.getHeight(null); double bili; if (width > 0) { bili = width / (double) w; height = (int) (h * bili); } else { if (height > 0) { bili = height / (double) h; width = (int) (w * bili); } } String srcImgPath = newFile.getAbsoluteFile().toString(); System.out.println(srcImgPath); BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = buffImg.createGraphics(); graphics.setBackground(new Color(255, 255, 255)); graphics.setColor(new Color(255, 255, 255)); graphics.fillRect(0, 0, width, height); graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(buffImg, "jpg", new File(srcImgPath)); System.out.println(oldFile.getName() + "文件处理完成"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newFile.getAbsolutePath(); } }