当前位置: 首页 > news >正文

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);}
}

相关文章:

  • 微服务--nacos+feign
  • NY197NY205美光闪存固态NY218NY226
  • 两个矩阵的卷积运算
  • 算法导论第五章:概率分析与随机算法的艺术
  • 篇章六 系统性能优化——资源优化——CPU优化(3)
  • 当空间与数据联动,会展中心如何打造智慧运营新范式?
  • 利用 Python 爬虫按关键字搜索 1688 商品
  • 学生端前端用户操作手册
  • Rust 学习笔记2025.6.13
  • python transformers库笔记(BertTokenizerFast类)
  • 阳台光伏配套电表ADL200N-CT/D16-Wf-1
  • 如何用4 种可靠的方法更换 iPhone(2025 年指南)
  • 8N65-ASEMI工业自动化领域专用8N65
  • Bean对象不同的方式注入,是不同的annotation接口描述
  • Volta 管理 Node 版本最佳实践教程
  • SpringBoot深度解析:从核心原理到最佳实践
  • Redis的string的底层实现原理
  • 使用 C/C++ 和 OpenCV DNN 进行人体姿态估计
  • [MSPM0开发]之七 MSPM0G3507 UART串口收发、printf重定向,循环缓冲解析自定义协议等
  • 编译,多面体库
  • 深圳福田做网站/开发一个app平台大概需要多少钱?
  • 青岛网站备案/发稿网
  • eclips怎么做网站/今天新闻联播
  • 昆山市有没有做网站设计的/深圳网站seo服务
  • asp网站怎么做/苹果自研搜索引擎或为替代谷歌
  • 武汉市人民政府官网/外贸seo优化