一、依赖包:
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency>
<groupId>com.madgag</groupId>
<artifactId>animated-gif-lib</artifactId>
<version>1.2</version>
</dependency>
二、代码示例
package com.fig;
import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.*;
import java.util.Iterator;
public class CommonTest {
public static void main(String[] args) {
String imgPath = "C:\\Users\\xxy\\Desktop\\图片\\微信图片_20200612095928.gif";
File source = new File(imgPath);
System.out.println(source.getAbsolutePath());
System.out.println(source.getName());
String newFilePath = "C:\\Users\\xxy\\Desktop\\图片\\new\\"+source.getName();
//
try {
zoomGifByQuality(imgPath,"gif",0.1F,newFilePath);
//zoomGifBySize(imgPath,"gif",76,123,newFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] zoomImg(){
String imgPath = "C:\\Users\\xxy\\Desktop\\图片\\8506f48e8d8fedbd8b972184992940be.jpg";
File source = new File(imgPath);
System.out.println("source file size====>"+getPrintSize(source.length()));
System.out.println(source.getAbsolutePath());
System.out.println(source.getName());
String newFilePath = "C:\\Users\\xxy\\Desktop\\图片\\new\\"+source.getName();
try {
Thumbnails.of(source).scale(1f).outputQuality(0.3f).toFile(newFilePath);
} catch (IOException e) {
e.printStackTrace();
}
File newFile = new File(newFilePath);
System.out.println("new file size====>"+getPrintSize(newFile.length()));
//将file转换成BufferedImage
//BufferedImage bimg &#