zoukankan      html  css  js  c++  java
  • Java基于animated-gif-lib的gif常用处理

      工作中, 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);
        }
    
    }
  • 相关阅读:
    Eclipse 控制台视图和服务器视图中停止Web服务器的差别
    JSP中forEach和forTokens循环的用法
    Java中的消息框
    JS弹出div简单样式
    Java中简单提示异常代码的行号,类名等
    Java简单的数据库连接
    Java简单方法批量修改Windows文件夹下的文件名(简单IO使用)
    Java中对文件的序列化和反序列化
    navicat 连接 mysql 出现Client does not support authentication protocol requested by server
    IoC是什么
  • 原文地址:https://www.cnblogs.com/nyatom/p/11118815.html
Copyright © 2011-2022 走看看