Java-based animated-gif-lib a common treatment gif

  Work, gif FIG moving image rotation / revolution gif picture set

  pom rely on very simple

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

  Simple tools to achieve

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

    / * * 
     * multi-picture rotation GIF 
     * @param imageList 
     * @param OutputPath 
     * @throws IOException 
     * / 
    static  void imagesToGif (List <the BufferedImage> imageList , String OutputPath) throws IOException {
         // split after a compression of a synthetic 
        AnimatedGifEncoder Encoder = new new AnimatedGifEncoder (); 
        encoder.start (OutputPath); 
        encoder.setRepeat ( 0 );
         for (the 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!" bufferedImage.getWidth (););
        } 
        // split after a compression of a synthetic 
        AnimatedGifEncoder Encoder = new new AnimatedGifEncoder (); 
        encoder.start (OutputPath); 
        encoder.setRepeat (decoder.getLoopCount ()); 
        for ( int I = decoder.getFrameCount () - . 1 ; I> = 0 ; i-- ) { 
            encoder.setDelay (decoder.getDelay (I)); // set the play delay time 
            BufferedImage BufferedImage = decoder.getFrame (I); // Get BufferedImage stream per frame 
            int height = bufferedImage.getHeight ();
             int width = 
            the 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);
    }

}

 

Guess you like

Origin www.cnblogs.com/nyatom/p/11118815.html
gif