<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
package jar;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ImageSmall {
public static void main(String[] args) {
File picture = new File("test.jpg");
BufferedImage sourceImg = null;
try {
sourceImg = ImageIO.read(new FileInputStream(picture));
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("图片大小=" + String.format("%.1f",picture.length()/1024.0));
System.out.println("图片宽=" + sourceImg.getWidth());
System.out.println("图片高=" + sourceImg.getHeight());
try {
Thumbnails.of(picture).size(8000, 8000)
.keepAspectRatio(true).outputFormat("jpg").toFile("test1.jpg");
} catch (IOException e) {
e.printStackTrace();
}
}
}