工作中, gif动图转图片/图片集转gif
pom依赖很简单
1<!-- gif --> 2 <dependency> 3 <groupId>com.madgag</groupId> 4 <artifactId>animated-gif-lib</artifactId> 5 <version>1.4</version> 6 </dependency>
简单的工具类实现
1import com.madgag.gif.fmsware.AnimatedGifEncoder; 2import com.madgag.gif.fmsware.GifDecoder; 3 4import javax.imageio.ImageIO; 5import java.awt.*; 6import java.awt.image.BufferedImage; 7import java.io.File; 8import java.io.IOException; 9import java.util.ArrayList; 10import java.util.List; 11 12public class GifOperator { 13 14 public static void main(String[] args) throws IOException { 15 16 String outputPath = "/home/lab/test/001.gif"; 17 String imagePath = "/home/lab/test/33.gif"; 18 reverseGif(imagePath,outputPath); 19 // Gif转图片 20 String dirPath = "/home/lab/test/22/"; 21 gifToImages(imagePath,dirPath); 22 List<BufferedImage> images = new ArrayList<>(); 23 for (int i = 0 ; i < 111;i++) { 24 File outFile = new File(dirPath + i + ".png"); 25 BufferedImage image = ImageIO.read(outFile); 26 images.add(image); 27 } 28 imagesToGif(images,"/home/lab/test/res.gif"); 29 } 30 31 /** 32 * 多图片转gif 33 * @param imageList 34 * @param outputPath 35 * @throws IOException 36 */ 37 static void imagesToGif(List<BufferedImage> imageList, String outputPath) throws IOException { 38 // 拆分一帧一帧的压缩之后合成 39 AnimatedGifEncoder encoder = new AnimatedGifEncoder(); 40 encoder.start(outputPath); 41 encoder.setRepeat(0); 42 for (BufferedImage bufferedImage : 43 imageList) { 44 encoder.setDelay(100); 45 int height = bufferedImage.getHeight(); 46 int width = bufferedImage.getWidth(); 47 BufferedImage zoomImage = new BufferedImage(width, height, 3); 48 Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); 49 Graphics gc = zoomImage.getGraphics(); 50 gc.setColor(Color.WHITE); 51 gc.drawImage(image, 0, 0, null); 52 encoder.addFrame(zoomImage); 53 } 54 encoder.finish(); 55 File outFile = new File(outputPath); 56 BufferedImage image = ImageIO.read(outFile); 57 ImageIO.write(image, outFile.getName(), outFile); 58 } 59 60 /** 61 * Gif转图片集 62 * @param imagePath 63 * @param outputDirPath 64 * @throws IOException 65 */ 66 static void gifToImages(String imagePath,String outputDirPath) throws IOException { 67 GifDecoder decoder = new GifDecoder(); 68 int status = decoder.read(imagePath); 69 if (status != GifDecoder.STATUS_OK) { 70 throw new IOException("read image " + imagePath + " error!"); 71 } 72 for (int i = 0; i < decoder.getFrameCount();i++) { 73 BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流 74 File outFile = new File(outputDirPath + i + ".png"); 75 ImageIO.write(bufferedImage, "png", outFile); 76 } 77 } 78 79 /** 80 * 视频倒放 81 * @param imagePath 82 * @param outputPath 83 * @throws IOException 84 */ 85 public static void reverseGif(String imagePath,String outputPath) throws IOException { 86 GifDecoder decoder = new GifDecoder(); 87 int status = decoder.read(imagePath); 88 if (status != GifDecoder.STATUS_OK) { 89 throw new IOException("read image " + imagePath + " error!"); 90 } 91 // 拆分一帧一帧的压缩之后合成 92 AnimatedGifEncoder encoder = new AnimatedGifEncoder(); 93 encoder.start(outputPath); 94 encoder.setRepeat(decoder.getLoopCount()); 95 for (int i = decoder.getFrameCount() -1; i >= 0; i--) { 96 encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间 97 BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流 98 int height = bufferedImage.getHeight(); 99 int width = bufferedImage.getWidth(); 100 BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType()); 101 Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); 102 Graphics gc = zoomImage.getGraphics(); 103 gc.setColor(Color.WHITE); 104 gc.drawImage(image, 0, 0, null); 105 encoder.addFrame(zoomImage); 106 } 107 encoder.finish(); 108 File outFile = new File(outputPath); 109 BufferedImage image = ImageIO.read(outFile); 110 ImageIO.write(image, outFile.getName(), outFile); 111 } 112 113}