如何在Java中保存.gif文件?[复制]

6ojccjat  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(113)

此问题已在此处有答案

Animating multiple images using Java(3个答案)
2天前关闭。
我想做一个皮肤创建器,使皮肤更容易为我的游戏,但我只知道如何使一个.png或类似的,并保存它。有没有一些方法,我可以有多个BufferedImage的,并保存到一个.gif文件与指定的FPS?

6l7fqoea

6l7fqoea1#

您可以使用ImageWriter.writeToSequence。以下示例中的saveAnimatedGIF方法说明了如何使用。

import javax.imageio.*;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

public class GifWriter {

    private static final int WIDTH = 1024;
    private static final int HEIGHT = 1024;

    private static final int MAX_FRAMES = 64;

    public static void main(String[] args) throws Exception {
        final List<BufferedImage> frames = new ArrayList<>();
        for (int i = 0; i < MAX_FRAMES; ++i) {
            frames.add(createDummyBufferedImage(i, MAX_FRAMES, WIDTH, HEIGHT));
        }

        try (FileOutputStream fos = new FileOutputStream("out.gif")) {
            saveAnimatedGIF(fos, frames);
        }
    }

    // Generate a dummy image for testing.
    private static BufferedImage createDummyBufferedImage(int i, int maxI, int w, int h) {
        final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                final double dx = (2.0 * x - w) / w;
                final double dy = (2.0 * y - h) / h;

                final int r = scale((dx + 1.0) / 2.0);
                final int g = scale((dy + 1.0) / 2.0);
                final int b = scale(Math.sqrt(dx*dx + dy*dy + (double)i / maxI));

                final int rgb = (r << 16) | (g << 8) | b;
                image.setRGB(x, y, rgb);
            }
        }

        return image;
    }

    private static int scale(double d) {
        final double d256 = d * 256.0;
        if (d < 0) {
            return 0;
        } else if (d >= 256.0) {
            return 255;
        } else {
            return (int)d256;
        }
    }

    public static void saveAnimatedGIF(OutputStream out, List<BufferedImage> frames) throws Exception {
        final ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();

        try (final ImageOutputStream ios = ImageIO.createImageOutputStream(out)) {

        iw.setOutput(ios);
        iw.prepareWriteSequence(null);

        for (BufferedImage frame : frames) {
            final ImageWriteParam iwp = iw.getDefaultWriteParam();
            final IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(frame), iwp);

            final IIOImage ii = new IIOImage(frame, null, metadata);
            iw.writeToSequence(ii, null);
        }

        iw.endWriteSequence();}
    }
}

相关问题