Java基于animated

Wesley13
• 阅读 672

  工作中, 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);
    }

}
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
Java使用Redis学习笔记
如果我们使用Java操作Redis,需要确保已经安装了redis服务及Javaredis驱动。Maven项目可以直接在pom.xml中加入jedis包驱动:<dependency<groupIdredis.clients</groupId<artifactI
Wesley13 Wesley13
2年前
Java开发者容易犯的十个错误
!(https://oscimg.oschina.net/oscnet/c9f00cc918684fbe8a865119d104090b.gif)Top1.数组转换为数组列表将数组转换为数组列表,开发者经常会这样做:\java\List<StringlistArrays.asList(arr);Arr
Stella981 Stella981
2年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Stella981 Stella981
2年前
ElasticSearch快速搭建java项目
1.创建springboot项目Pom文件引入elasticsearch依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterdataelasticsearch</artifactI
Stella981 Stella981
2年前
Jenkins流水线即代码之扩展共享库
!(https://oscimg.oschina.net/oscnet/ab8ee75c43cb1a3fd0fac241648861b03c5.gif)!(https://oscimg.oschina.net/oscnet/1a35fdf03222f188f706711d2b43eae6a14.gif)!(https://osci
Stella981 Stella981
2年前
Maven 常用命令,你都会几个
!(https://oscimg.oschina.net/oscnet/772a49ea4d09920fdf0651e8c5662a12665.gif)点击上方【村雨遥】添加关注!(https://oscimg.oschina.net/oscnet/91f5066581719dd3c513dcf35da32289482.gif)目录
Stella981 Stella981
2年前
Python图片转gif(将静态图转化为分块加载的动态图)
简介将静态图转化为分块加载的动态图方案1.PIL:1.创建背景图2.将原图拆分成N块并依次合成到背景图的相应位置,得到N张素材图3.将N张素材图合成GIF2.pygifsicle对合成的GIF进行优化
燕青 燕青
9个月前
AE插件:GifGun for Mac 汉化版
是一款安装在AfterEffects中使用的AE快速输出GIF动图格式插件,你可以使用gifgun插件直接输出GIF动画格式,支持自定义GIF文件的大小、帧数率等各种属性
燕青 燕青
6个月前
GIF制作工具:GIFfun for Mac中文激活版
是一款可以在几秒钟内完成转GIF动画转换的GIF编辑器软件。在Mac上,GIFfun可以帮助用户从日常生活中的照片和视频中直接取材来创建动画GIF,并且可以将多张照片转换为GIF,还可以将多张照片转换为视频。同时,它也支持批量处理,可以进行GIF转视频、视
流浪剑客 流浪剑客
6个月前
GIF制作工具:GIFfun
是一款可以在几秒钟内完成转GIF动画转换的GIF编辑器软件,它可以帮助用户从日常生活中的照片和视频中直接取材来创建动画GIF。在功能方面,GIFfunMac版可以将多张照片转换为GIF,还可以将多张照片转换为视频,并且支持批量处理。此外,它也提供了GIF转