Pay tribute to the father of Gif, use Java to generate Gif pictures

Click to follow Brother Qiang to see more exciting articles

Hello, everyone, I'm Brother Qiang.

According to US media reports, American computer scientist and inventor of the GIF image format Stephen Wilhite (Stephen Wilhite) died of new coronary pneumonia on March 14, local time, at the age of 74. In 1987, he created the Graphics Interchange Format (GIF) to compress images; in 2012, GIF was named Word of the Year by the Oxford American Dictionary; in 2013, Stephen received the Webby Lifetime Achievement Award for inventing GIF.

First of all, I am sorry for the passing of the father of GIF.

GIF pictures are a very widely used picture format in our daily life, especially those emoticons full of wisdom. If there is no GIF, there will be no soul.

Today, Brother Qiang is here to teach you how to use Java to generate pictures in GIF format to pay tribute to the great man.

hands-on

Here, we do not explain too much about the original GIF, but directly practice.

guide package

We can do this using an open source GIF generation toolkit. Here animated-gif-lib, the specific project address is as follows:

github.com/rtyley/anim…

Import the Maven package into the project:

<dependency>
  <groupId>com.madgag</groupId>
  <artifactId>animated-gif-lib</artifactId>
  <version>1.4</version>
</dependency>
复制代码

code development

With the toolkit, generating GIF images is very simple. You only need to AnimatedGifEncoderconfigure the playback interval, repetition times, and image generation path of some pictures to generate the desired GIF pictures~

The specific code is as follows:

import com.madgag.gif.fmsware.AnimatedGifEncoder;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

public class GifDemo {

  public static void main(String[] args) throws Exception {
    BufferedImage image1 = ImageIO.read(new File(GifDemo.class.getResource("/static/1.jpeg").getFile()));
    BufferedImage image2 = ImageIO.read(new File(GifDemo.class.getResource("/static/2.jpeg").getFile()));
    BufferedImage image3 = ImageIO.read(new File(GifDemo.class.getResource("/static/3.jpeg").getFile()));
    AnimatedGifEncoder e = new AnimatedGifEncoder();
    // 设置生成图片大小
    e.setSize(900, 1000);
    //生成的图片路径
    e.start(new FileOutputStream("./testgif.gif"));
    //图片之间间隔时间
    e.setDelay(500); 
    //重复次数 0表示无限重复 默认不重复
    e.setRepeat(0);
    //添加图片
    e.addFrame(image1);
    e.addFrame(image2);
    e.addFrame(image3);
    e.finish();
  }

}
复制代码

The above code is very simple, AnimatedGifEncoderthere are many simple methods provided in the class:

  • setSizeSet the generated image size
  • startSet the generated image path and open the output stream
  • setDelaytime between pictures
  • setRepeatThe number of repetitions is 0, which means infinite repetition and no repetition by default.

run and see

Here we have prepared three pictures under the classpath:

Run the above code, generate the picture in the current directory, open the generated picture with a browser, and the effect is as follows (here I set the picture size due to platform limitations):

Haha, it's beautiful, it's easy to generate GIF images in Java. In this case, hehe, if there are good-looking pictures in the future, if you dislike too many pictures and it is difficult to organize them, you can directly use the code to get them into a GIF. I'm really a careful little Jiujiu.

Brother Qiang has something to say

Well, through the above simple method, we have realized the generation of GIF pictures in Java. Of course, this is the result that can only be achieved by standing on the shoulders of predecessors. As for the specific details, those who are lazy and need the code directly can follow the public account "Qiangge Nao Nao Nao" and reply "GIF" in the background to get the specific code of the project.

Click to follow Brother Qiang to see more exciting articles

Guess you like

Origin juejin.im/post/7080121832480702478
gif