引入依赖
<dependency> <groupId>com.github.jai-imageio</groupId> <artifactId>jai-imageio-core</artifactId> <version>1.4.0</version> </dependency>
工具类
指定图片文件夹,指定生成的gif文件全名路径(可使用本地路径或共享路径)
可指定每帧延迟时间(调整一下,可以设定每一帧的延迟时间)
import javax.imageio.ImageIO; import javax.imageio.ImageWriter; import javax.imageio.ImageTypeSpecifier; import javax.imageio.metadata.IIOInvalidTreeException; import javax.imageio.metadata.IIOMetadata; import javax.imageio.metadata.IIOMetadataNode; import javax.imageio.stream.ImageOutputStream; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; public class GifCreator { /** * 将指定文件夹内的所有图片拼接成一个 GIF 文件。 * * @param inputFolder 输入文件夹路径 * @param outputGif 输出 GIF 文件路径 * @param delay 每帧之间的延迟时间(单位:毫秒) * @throws IOException 如果读取或写入文件时发生错误 */ public static void createGifFromImages(String inputFolder, String outputGif, int delay) throws IOException { File folder = new File(inputFolder); File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".jpg")); if (files == null || files.length == 0) { throw new IOException("No image files found in the specified folder."); } // 按文件名排序,要求文件名称需要是数值 Arrays.sort(files, Comparator.comparingDouble(f-> { String name = f.getName(); return Double.parseDouble(name.substring(0, name.indexOf("."))); })); ImageWriter writer = ImageIO.getImageWritersByFormatName("gif").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File(outputGif)); writer.setOutput(ios); // 创建元数据 IIOMetadata metadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB), null); // 设置每帧的延迟时间 setDelayTime(metadata, delay); // 开始写入序列 writer.prepareWriteSequence(metadata); for (File file : files) { BufferedImage image = ImageIO.read(file); System.out.println("当前写入图片:" + file.getName()); writer.writeToSequence(new javax.imageio.IIOImage(image, null, metadata), null); } // 结束写入序列 writer.endWriteSequence(); writer.dispose(); ios.close(); } /** * 设置每帧的延迟时间。 * * @param metadata 元数据对象 * @param delay 每帧之间的延迟时间(单位:毫秒) */ private static void setDelayTime(IIOMetadata metadata, int delay) throws IIOInvalidTreeException { String metaFormatName = metadata.getNativeMetadataFormatName(); IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metaFormatName); IIOMetadataNode graphicsControlExtensionNode = new IIOMetadataNode("GraphicControlExtension"); graphicsControlExtensionNode.setAttribute("disposalMethod", "none"); graphicsControlExtensionNode.setAttribute("userInputFlag", "false"); graphicsControlExtensionNode.setAttribute("transparentColorFlag", "false"); graphicsControlExtensionNode.setAttribute("delayTime", Integer.toString(delay / 10)); // 单位是1/100秒 graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0"); root.appendChild(graphicsControlExtensionNode); metadata.setFromTree(metaFormatName, root); } public static void main(String[] args) { try { // 设定本地文件路径 // String inputFolder = "D:/test/c"; // String outputGif = "D:/test/g/e.gif"; // 设定共享文件夹路径 String inputFolder = "//ip/Users/Public/共享文件/xxx/source"; String outputGif = "//ip/Users/Public/共享文件/xxx/gif/test.gif"; int delay = 5000; // 每帧之间的延迟时间(单位:毫秒) createGifFromImages(inputFolder, outputGif, delay); System.out.println("GIF created successfully at " + outputGif); } catch (IOException e) { e.printStackTrace(); } } }
标签:imageio,String,delay,gif,文件夹,import,java,javax,metadata From: https://www.cnblogs.com/uoky/p/18641702