The code looks good, I like it
package com.dayu.test; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; public class ImgCompress { private Image img; private int width; private int height; // image path public static final String READ_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\sssssssss\\" ; // output image Path public static final String WRITE_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\sssssssss\\output" ; /** * Constructor, read file */ public ImgCompress(String fileName) throws IOException { File file = new File(fileName); // Read in the file img = ImageIO.read(file); // Construct Image object width = img.getWidth( null ); // Get the source image width height = img.getHeight( null ); // get the source image length } @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { System.out.println( "Start:" + new Date().toLocaleString()); // The file name is renamed, I have 10 pictures here, named 1-10.jpg at a time for ( int i = 1; i <= 10; i++ ) { String fileName=i+".jpg"; ImgCompress imgCom = new ImgCompress(READ_FILE_PATH +fileName); imgCom.resizeFix(1500, 1500,fileName); } System.out.println("结束:" + new Date().toLocaleString()); } /** * Compress by width or height * @param w int max width * @param h int max height * @param fileName */ public void resizeFix(int w, int h, String fileName) throws IOException { if (width / height > w / h) { resizeByWidth(w,fileName); } else { resizeByHeight(h,fileName); } } /** * Based on the width, scale the image proportionally * * @param w int new width * @param fileName */ public void resizeByWidth(int w, String fileName) throws IOException { int h = (int) (height * w / width); resize(w, h,fileName); } /** * Based on height, scale the picture in equal proportion * * @param h int new height * @param fileName */ public void resizeByHeight(int h, String fileName) throws IOException { int w = (int) (width * h / height); resize(w, h,fileName); } /** * Force compress/enlarge image to fixed size * * @param w int new width * @param h int new height */ public void resize( int w, int h,String fileName) throws IOException { // The thumbnail algorithm of SCALE_SMOOTH generates the smoothness of the thumbnail image with priority over the speed of the generated image quality comparison Good but slow BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(img, 0, 0, w, h, null ); // draw the reduced image File file= new File(WRITE_FILE_PATH); if (! file.exists()){ file.mkdirs(); } String newFilePath = WRITE_FILE_PATH+File.separator+fileName; File destFile = new File(newFilePath); FileOutputStream out = new FileOutputStream(destFile); // Output to file stream // Bmp, png, gif can be converted to jpg normally JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); // JPEG编码 out.close(); } }