Java基于animated

Wesley13
• 阅读 539

  工作中, gif动图转图片/图片集转gif

  pom依赖很简单

<!-- gif -->
        <dependency>
            <groupId>com.madgag</groupId>
            <artifactId>animated-gif-lib</artifactId>
            <version>1.4</version>
        </dependency>

  简单的工具类实现

import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.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 outputPath = "/home/lab/test/001.gif";
        String imagePath = "/home/lab/test/33.gif";
        reverseGif(imagePath,outputPath);
        // Gif转图片
        String dirPath = "/home/lab/test/22/";
        gifToImages(imagePath,dirPath);
        List<BufferedImage> images = new ArrayList<>();
        for (int i = 0 ; i < 111;i++) {
            File outFile = new File(dirPath + i + ".png");
            BufferedImage image = ImageIO.read(outFile);
            images.add(image);
        }
        imagesToGif(images,"/home/lab/test/res.gif");
    }

    /**
     * 多图片转gif
     * @param imageList
     * @param outputPath
     * @throws IOException
     */
    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(100);
            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
     */
    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);
    }

}
点赞
收藏
评论区
推荐文章
Chase620 Chase620
2年前
前端GIF生成及优化
羚珑平台的动图可视化设计之前只支持mp4的导出,但在业务方使用场景中大部分需要投放GIF。故本文主要介绍使用gif.js生成GIF时遇到的一些问题、GIF压缩以及GIF的文件格式和对应编码在gif.js是如何实现的。GIF文件结构介绍位图图形文件格式,采用8位(256色)重现真彩色的图像。它实际上是一种压缩文档,采用LZW压缩算
Wesley13 Wesley13
1年前
Java使用Redis学习笔记
如果我们使用Java操作Redis,需要确保已经安装了redis服务及Javaredis驱动。Maven项目可以直接在pom.xml中加入jedis包驱动:<dependency<groupIdredis.clients</groupId<artifactI
Wesley13 Wesley13
1年前
Java开发者容易犯的十个错误
!(https://oscimg.oschina.net/oscnet/c9f00cc918684fbe8a865119d104090b.gif)Top1.数组转换为数组列表将数组转换为数组列表,开发者经常会这样做:\java\List<StringlistArrays.asList(arr);Arr
Stella981 Stella981
1年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Stella981 Stella981
1年前
ElasticSearch快速搭建java项目
1.创建springboot项目Pom文件引入elasticsearch依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterdataelasticsearch</artifactI
Stella981 Stella981
1年前
Android中显示动画的GIF
首先看看GIF文件的存储结构和动画原理\转\GIF图片的文件储存结构和动画原理(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.sina.com.cn%2Fs%2Fblog_571296860100jvhz.html)以及已知的两种解决方案Android&
Stella981 Stella981
1年前
Jenkins流水线即代码之扩展共享库
!(https://oscimg.oschina.net/oscnet/ab8ee75c43cb1a3fd0fac241648861b03c5.gif)!(https://oscimg.oschina.net/oscnet/1a35fdf03222f188f706711d2b43eae6a14.gif)!(https://osci
Stella981 Stella981
1年前
Maven 常用命令,你都会几个
!(https://oscimg.oschina.net/oscnet/772a49ea4d09920fdf0651e8c5662a12665.gif)点击上方【村雨遥】添加关注!(https://oscimg.oschina.net/oscnet/91f5066581719dd3c513dcf35da32289482.gif)目录
Stella981 Stella981
1年前
Python图片转gif(将静态图转化为分块加载的动态图)
简介将静态图转化为分块加载的动态图方案1.PIL:1.创建背景图2.将原图拆分成N块并依次合成到背景图的相应位置,得到N张素材图3.将N张素材图合成GIF2.pygifsicle对合成的GIF进行优化
Stella981 Stella981
1年前
Js使用面向对象和面向过程的方法实现拖拽物体的效果
1.面向过程的拖拽实现代码:!(https://oscimg.oschina.net/oscnet/d680c759957babef2fec0902676eaa35ad9.gif)<!DOCTYPEhtml<html<head<titledragDiv</title