¶Ô±ÈÐÂÎļþ |
| | |
| | | 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")); |
| | | } |
| | | 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<ImageWriter> 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 && originalImage.getWidth()>w) { |
| | | 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); |
| | | } |
| | | } else { |
| | | //ä¸å¤ç |
| | | } |
| | | } 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(); |
| | | } |
| | | } |
| | | } |