java 将多张图片合成gif动态图
依赖
<!--maven中引入--><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version></dependency>
代码
import cn.hutool.core.img.gif.AnimatedGifEncoder;
import cn.hutool.core.img.gif.GifDecoder;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class GifOperator {public static void main(String[] args) throws IOException {String dirPath = "D:/cs/image/";//文件地址List<BufferedImage> images = new ArrayList<>();
// for (int i = 5 ; i < 55;i++) {for (int i = 54 ; i > 5;i--) {File outFile = new File(dirPath + i + ".png");BufferedImage image = ImageIO.read(outFile);images.add(image);}//images代表多张图片 后者代表成功的gif图片imagesToGif(images,"D:/cs/image/res_200.gif");}/*** 多图片转gif* @param imageList* @param outputPath* @throws IOException*/public static void imagesToGif(List<BufferedImage> imageList, String outputPath) throws IOException {// 拆分一帧一帧的压缩之后合成AnimatedGifEncoder encoder = new AnimatedGifEncoder();encoder.start(outputPath);encoder.setRepeat(0);for (BufferedImage bufferedImage :imageList) {encoder.setDelay(200);int height = bufferedImage.getHeight();int width = bufferedImage.getWidth();BufferedImage zoomImage = new BufferedImage(width, height, 3);Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);Graphics gc = zoomImage.getGraphics();gc.setColor(Color.WHITE);gc.drawImage(image, 0, 0, null);encoder.addFrame(zoomImage);}encoder.finish();File outFile = new File(outputPath);BufferedImage image = ImageIO.read(outFile);ImageIO.write(image, outFile.getName(), outFile);}/*** Gif转图片集* @param imagePath* @param outputDirPath* @throws IOException*/public static void gifToImages(String imagePath,String outputDirPath) throws IOException {GifDecoder decoder = new GifDecoder();int status = decoder.read(imagePath);if (status != GifDecoder.STATUS_OK) {throw new IOException("read image " + imagePath + " error!");}for (int i = 0; i < decoder.getFrameCount();i++) {BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流File outFile = new File(outputDirPath + i + ".png");ImageIO.write(bufferedImage, "png", outFile);}}/*** 视频倒放* @param imagePath* @param outputPath* @throws IOException*/public static void reverseGif(String imagePath,String outputPath) throws IOException {GifDecoder decoder = new GifDecoder();int status = decoder.read(imagePath);if (status != GifDecoder.STATUS_OK) {throw new IOException("read image " + imagePath + " error!");}// 拆分一帧一帧的压缩之后合成AnimatedGifEncoder encoder = new AnimatedGifEncoder();encoder.start(outputPath);encoder.setRepeat(decoder.getLoopCount());for (int i = decoder.getFrameCount() -1; i >= 0; i--) {encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流int height = bufferedImage.getHeight();int width = bufferedImage.getWidth();BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType());Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);Graphics gc = zoomImage.getGraphics();gc.setColor(Color.WHITE);gc.drawImage(image, 0, 0, null);encoder.addFrame(zoomImage);}encoder.finish();File outFile = new File(outputPath);BufferedImage image = ImageIO.read(outFile);ImageIO.write(image, outFile.getName(), outFile);}
}