当前位置: 首页 > 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);}
}
http://www.xdnf.cn/news/1017001.html

相关文章:

  • 国产数据库StarRocks在数栈轻量化数据开发的全流程实践
  • 普通人怎样用好Deepseek?
  • MySQL 8.0 OCP 英文题库解析(十九)
  • 26-数据结构-线性表2
  • linux alignment fault对齐造成设备挂死问题定位梳理
  • Leetcode 2604. 吃掉所有谷子的最短时间
  • 线性回归原理推导与应用(九):逻辑回归多分类问题的原理与推导
  • 用户通知服务,轻松实现应用与用户的多场景交互
  • 嵌套滚动交互处理总结
  • FastChat 架构拆解:打造类 ChatGPT 私有化部署解决方案的基石
  • python实现鸟类识别系统实现方案
  • Java实现Pdf转Word
  • 打破语言壁垒!DHTMLX Gantt 与 Scheduler 文档正式上线中文等多语言版本!
  • 使用 PolarProxy+Proxifier 解密 TLS 流量
  • 北京大学肖臻老师《区块链技术与应用》公开课:08-BTC-比特币挖矿
  • MySQL索引原理
  • KDJ指标的运用
  • 商家如何利用Shopify插件进行AB测试和优化
  • MAC无法 ping 通github 系列主页
  • EFK架构的数据安全性
  • AI编程第一步:零基础用人工智能生成你的Hello World和计算器
  • SQL力扣
  • 【AI News | 20250613】每日AI进展
  • 使用若依框架新建模块后导入UI项目目录对应前端文件后报找不到文件错误处理
  • 【DVWA系列】——xss(Stored)——High详细教程
  • 高精度算法详解:从原理到加减乘除的完整实现
  • 【AI图像生成网站Golang】部署图像生成服务(阿里云ACK+GPU实例)
  • skynet源码学习-skynet_mq队列
  • 目标检测标注格式
  • 对象映射 C# 中 Mapster 和 AutoMapper 的比较