GIF image dynamic generation - JAVA background generation

background

       There are many interesting scenes on the Internet, one of which is GIF. This is not a video, but a GIF image information. Although there is no sound, it has brought us endless fun. If Doutu is the fun of chatting or online chatting, then the animation must be the technical principle of this joy.

      The full name of GIF is Graphics Interchange Format, which can be translated into Graphics Interchange Format , used to display indexed color images in Hypertext Markup Language (Hypertext Markup Language), and is widely used on the Internet and other online service systems. GIF is a public image file format standard copyrighted by Compu Serve Corporation.

      So what technology can be used to generate these GIF images? Today I will share a JAVA implementation based on the open source library AnimatedGifEncoder to dynamically build a GIF gallery. After mastering this technology, coupled with your genius mind, you will definitely be able to display unique creativity and create content full of wisdom or mind-opening. Without further ado, let's start.

features

      Image files in GIF format have the following characteristics:

(1) The extension of the image file in GIF format is ".gif";

(2) Performs best for grayscale images;

(3) There are two versions, GIF87a and GIF89a;

(4) Using the improved LZW compression algorithm to process image data;

(5) Palette data is divided into general palette and local palette, with different color values;

(6) Does not support 24bit color mode, and can store up to 256 colors.

use

       ①GIF is a file in compressed format, which is used to reduce the time of file transfer on the network;

       ②The bit depth of GIF is 1-8bit, monochrome and transparent, realized by a palette of up to 256 colors, and the image size is up to 64K×64K pixels. GIF is mainly a transmission format designed for a data stream, not as a file storage format, so it is the most complex image file format;

       ③Support Bitmap, Grayscale and index color modes.

Introduction to AnimatedGifEncoder

      AnimatedGifEncoder was written by Kevin Weiner, and its author authorizes everyone to use this code in any way, but it should be noted that the LZW algorithm used in the code is patented by Unisys. However, given that this patent expired in most countries and regions in 2006, it should be safe to use now.

GIF Creation Generation

1. Create a maven project

      Create a new maven project and import related resource packages. The key code is as follows:

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

2. Custom generation

      Custom generation refers to directly using the method created by the system to generate a gif, without using external resources such as pictures and videos, and directly drawing a GIF on the interface. The key code is given below:

package com.yelang.mp42gif;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import com.madgag.gif.fmsware.AnimatedGifEncoder;

public class PageTest {

	public static final int SIZE = 200;
	 
    public static void main(String[] args) throws IOException {
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start("D:/giftest/out.gif");
        encoder.setTransparent(Color.WHITE);
        encoder.setRepeat(0);
        encoder.setDelay(50);
 
        BufferedImage img = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g2d = img.createGraphics();
 
        for (int i=0; i<100; i++) {
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, SIZE, SIZE);
            g2d.setColor(Color.BLACK);
            g2d.drawOval(0, i, 120, 120);
            encoder.addFrame(img);
        }
 
        g2d.dispose();
        encoder.finish();
        System.out.println("完成");
    }

}

       After the execution is completed, under the target folder, you can see that the gif image is output.

      Drag and drop the out.gif file into the browser, you can see the dynamic effect, as shown in the figure below:

3. Customize the synthesis of multiple pictures

       In some requirements, such as synthesizing a gif image based on multiple given images (such as the following four 1-4.jpg). How to realize this demand? The solution to this need is highlighted here.

1. Define the original data source

BufferedImage image1 = ImageIO.read(new File("D:/giftest/dir2/1.jpg"));
BufferedImage image2 = ImageIO.read(new File("D:/giftest/dir2/2.jpg"));
BufferedImage image3 = ImageIO.read(new File("D:/giftest/dir2/3.jpg"));
BufferedImage image4 = ImageIO.read(new File("D:/giftest/dir2/4.jpg"));

The above code converts the original image into an input stream for use

2. GIF object settings

 AnimatedGifEncoder e = new AnimatedGifEncoder();
// 设置生成图片大小
e.setSize(4000, 3000);
//生成的图片路径
	    
e.start("D:/giftest/dir2/demo1.gif");
//图片之间间隔时间
e.setDelay(500); 
//重复次数 0表示无限重复 默认不重复
e.setRepeat(0);
e.setQuality(5);

3. Image filling

//添加图片
e.addFrame(image1);
e.addFrame(image2);
e.addFrame(image3);
e.addFrame(image4);
e.finish();

 The completed code is as follows:

public static void test3() throws IOException {
		BufferedImage image1 = ImageIO.read(new File("D:/giftest/dir2/1.jpg"));
	    BufferedImage image2 = ImageIO.read(new File("D:/giftest/dir2/2.jpg"));
	    BufferedImage image3 = ImageIO.read(new File("D:/giftest/dir2/3.jpg"));
	    BufferedImage image4 = ImageIO.read(new File("D:/giftest/dir2/4.jpg"));
	    AnimatedGifEncoder e = new AnimatedGifEncoder();
	    // 设置生成图片大小
	    e.setSize(4000, 3000);
	    //生成的图片路径
	    
	    e.start("D:/giftest/dir2/demo1.gif");
	    //图片之间间隔时间
	    e.setDelay(500); 
	    //重复次数 0表示无限重复 默认不重复
	    e.setRepeat(0);
	    e.setQuality(5);
	    //添加图片
	    e.addFrame(image1);
	    e.addFrame(image2);
	    e.addFrame(image3);
	    e.addFrame(image4);
	    e.finish();
	    System.out.println("finish");
	}

 After the execution is complete, you can see the generated GIF as follows:

Digression - Missing the Giants

      The inventor of GIF is Stephen Wilhite, an American computer scientist and inventor of the GIF image format. According to US media reports, on March 14 local time, Stephen Wilheit, the inventor of gif, died of the epidemic at the age of 74. His wife, Catherine, mentioned in an interview that his family was by his side before Wilhelm died, and also mentioned in the obituary that "despite his achievements, he was still a very humble and kind man. good man."

Summarize

      This article briefly introduces the knowledge of GIF images, and takes JAVA technology as an example, introduces the technology of generating GIF in the background, and provides more detailed code examples, hoping to help you. Finally, I miss the inventor of GIF, Stephen Wilheit, who passed away due to the new crown infection. Thanks to this era, we stand on the shoulders of countless giants, so that we can concentrate on enjoying the pleasure brought by these technologies.

Guess you like

Origin blog.csdn.net/yelangkingwuzuhu/article/details/126805813