package com.doumee.core.utils; import com.alibaba.fastjson.JSONObject; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.utils.aliyun.ALiYunUtil; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.UUID; public class FaceImageCompress { @Autowired private SystemDictDataBiz systemDictDataBiz; public static void main(String[] args) { // compressAllFiles(new File("/usr/local/ftp/member/20240317"),800); compressAllFiles(new File("D://visit"),1100); } public static void compressAllFiles(File folder ,int w) { File[] files = folder.listFiles(); for (File file : files) { if (file.isDirectory()) { // getAllFiles(file); } else { compressImg(file,w); System.out.println(file.getAbsolutePath()); } } } public static void compressImageNew(File input, long targetSize) { try { BufferedImage image = ImageIO.read(input); // 使用IIOImage封装BufferedImage IIOImage iioImage = new IIOImage(image, null, null); // 获取所有可用的ImageWriter Iterator imageWriters = ImageIO.getImageWritersByFormatName("jpeg"); if (!imageWriters.hasNext()) { throw new IOException("No JPEG image writers found"); } String name = input.getAbsolutePath(); copyToNewfile(input); input.delete(); ImageWriter imageWriter = imageWriters.next(); File output = new File(name); // 设置压缩参数 ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality(1f); // 初始质量为最高 // 写入文件前计算目标文件大小 long fileSize = 0; do { // 清空写入器以便重用 imageWriter.reset(); // 设置新的压缩质量 float quality = imageWriteParam.getCompressionQuality(); imageWriteParam.setCompressionQuality(quality - (quality * 2) / 100); // 降低质量 // 写入文件 imageWriter.setOutput(ImageIO.createImageOutputStream(output)); imageWriter.write(null, iioImage, imageWriteParam); // 更新文件大小 fileSize = output.length(); // 检查文件大小是否满足要求 } while (fileSize > targetSize && imageWriteParam.getCompressionQuality() > 0); imageWriter.dispose(); } catch (IOException e) { e.printStackTrace(); } } public static void compressImg(File file, int w) { try { if(w <500){ return; } long originalFileSize = file.length(); BufferedImage originalImage = ImageIO.read(file); double compressionRatio = (double) originalFileSize / (200 * 1024.0); if (compressionRatio >= 1 ) { System.out.println("==========================="+compressionRatio); // int compressedWidth = (int) (originalImage.getWidth() / compressionRatio); // int compressedHeight = (int) (originalImage.getHeight() / compressionRatio); int compressedWidth = w; double tt =(double)w / (double)originalImage.getWidth(); int compressedHeight = (int) (originalImage.getHeight() * tt); BufferedImage compressedImage = new BufferedImage(compressedWidth, compressedHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = compressedImage.createGraphics(); graphics.drawImage(originalImage, 0, 0, compressedWidth, compressedHeight, null); // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // ImageOutputStream imOut = ImageIO.createImageOutputStream(byteArrayOutputStream); // ImageIO.write(compressedImage, "jpg", imOut); // InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); graphics.dispose(); String oldFile= copyToNewfile(file); String name = file.getAbsolutePath(); file.delete(); File outputfile= new File(name); ImageIO.write(compressedImage, "jpg", outputfile); if(outputfile.length()<50 * 1024.0){ resetOldfile(oldFile); System.out.println("================小于50"); } } else { //不处理 System.out.println("================成功"); } } catch (Exception e) { e.printStackTrace(); } } public static String copyToNewfile(File file ){ try { String oldFile = file.getAbsolutePath(); String newFile = oldFile.replace(file.getName(),"copy/"+file.getName()); Path sourcePath = Paths.get(oldFile); Path destinationPath = Paths.get( newFile); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); return newFile; } catch (Exception e) { e.printStackTrace(); } return null; } public static void resetOldfile(String oldFile ){ try { String newFile= oldFile.replace( "copy/",""); Path sourcePath = Paths.get(oldFile); Path destinationPath = Paths.get( newFile); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); } catch (Exception e) { e.printStackTrace(); } } }