java实现多张图片合成gif动态图片

java实现多张图片合成gif动态图片

由于工作需要 需要把多张图片合成一个gif动态图片 然后各种面向百度编程 借鉴了一下该作者的文章 原作者[Java基于animated-gif-lib的gif常用处理]
1.使用工具类

public class GifOperator {

    public static void main(String[] args) throws IOException {
        String dirPath = "D:/demo/";//文件地址
        List<BufferedImage> images = new ArrayList<>();
        for (int i = 1 ; i < 6;i++) {
            File outFile = new File(dirPath + i + ".jpg");
            BufferedImage image = ImageIO.read(outFile);
            images.add(image);
        }
        //images代表多张图片 后者代表成功的gif图片
        imagesToGif(images,"D:/demo/res_200.gif");
    }

    /**
     * 多图片转gif
     * @param imageList
     * @param outputPath
     * @throws IOException
     */
    public static void imagesToGif(List<BufferedImage> imageList, String outputPath) throws IOException {
        // 拆分一帧一帧的压缩之后合成
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(outputPath);
        encoder.setRepeat(0);
        for (BufferedImage bufferedImage :
                imageList) {
            encoder.setDelay(200);
            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
     */
    public 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!");
        }
        // 拆分一帧一帧的压缩之后合成
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(outputPath);
        encoder.setRepeat(decoder.getLoopCount());
        for (int i = decoder.getFrameCount() -1; i >= 0; i--) {
            encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间
            BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            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);
    }

}

2.导入代码后 缺失pom依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.8</version>
</dependency>

另外 本地idea跑着好好的 部署到服务器出现了一个问题

java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
	at org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor.postProcessBeanFactory(BusWiringBeanFactoryPostProcessor.java:106)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:181)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
	at cn.xzcysoft.cy.Application.main(Application.java:18)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:86)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:566)
	at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
	... 20 common frames omitted

然后百度一下 好像是jdk版本兼容问题
解决方案

<!--maven中引入-->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Unity是一款强大的游戏引擎,在游戏制作和开发中有着广泛的应用。然而,Unity也可以用于制作动画和GIF。 如果想要将多张图片合成GIF,我们可以使用Unity中的Animation窗口来完成。具体步骤如下: 1. 将所有需要合成图片导入到Unity中,可以使用菜单栏中的Assets -> Import New Asset来导入图片。 2. 在Unity的Hierarchy面板中创建一个空物体,命名为“合成器”,并选择该物体。 3. 在Unity的Animation窗口中点击“Create”按钮,创建一个新的Animaton Clip,并将其命名为“GIF”。 4. 在Animation窗口中,点击“Record”按钮,开始录制动画。 5. 在录制状态下,将需要合成图片依次拖拽到场景中,使其按一定的顺序排列。 6. 当所有图片都排列好后,停止录制动画。 7. 在Animation窗口中点击“Play”按钮,预览合成后的GIF动画。 8. 如果需要将GIF导出,可以使用菜单栏中的Assets -> Export Package来将“合成器”物体和“GIF”动画一起导出。 通过上述步骤,我们就能够在Unity中快速地将多张图片合成GIF动画。除了Animation窗口,Unity中还有其他一些插件和工具可以帮助我们创建更复杂、更高质量的动画。 ### 回答2: 在Unity中,将多张图片合成GIF是一项很简单的任务。通过使用适当的插件和脚本,您可以轻松地将您的图片或动画序列转换为动画GIF。 首先,你需要导入一个GIF制作插件。在Unity Asset Store中,有许多可用的插件,例如GIF压缩机和Eazy GIF。这些插件可以帮助你将多张图片转换为GIF。 其次,将您的图片导入Unity,将它们设置为一个能够组合图像帧的Sprite纹理。可以使用Unity内置的Sprite组件将图片导入图像并创建Sprite。或是使用导入的插件,都可以方便地将图片转换成适当的组合帧。 然后,创建一个GIF并设置其属性。您可以使用GIF制作插件(例如GIF Compression机)创建新的GIF,并根据您的需要设置其属性。在此过程中,您可以设置GIF的间隔时间、帧速率等等。这些设置将影响您最终生成的GIF的质量和文件大小。 最后,将您的图片添加到GIF中,以生成一个连续的动画序列。在Unity中,您可以使用代码编写脚本,将您的纹理合成一个GIF。使用SetPixels和Apply命令,您可以组成您的图片,并导出GIF。您还可以使用插件来完成此任务。 综上所述,Unity 多张图片合成GIF 只需几个简单的步骤即可完成。通过使用适当的插件和脚本,您可以轻松地将您的图片序列转换为动画GIF,以便在您的项目中使用并分享。 ### 回答3: Unity可以通过使用插件或脚本实现多张图片合成GIF动画。其中,最常用的插件是“EZ GIF”,它可以快速、轻松地生成高质量的GIF动画。 首先,需要将所有需要合成图片导入到Unity的资源管理器中。然后,使用插件或脚本在代码中将这些图片按顺序合成为一张GIF动画。在合成完成后,可以将GIF动画保存到硬盘或直接在游戏中播放。 在合成GIF动画时,需要注意以下几点: 1. 图片的大小和分辨率应尽可能一致,避免出现变形或缩放。 2. 图片的帧率和动画长度应根据需要谨慎选择,避免动画过长或过短。 3. 合成GIF动画可能会消耗大量的系统资源和内存,因此需要合理优化代码和程序运行效率。 总之,利用Unity的插件或脚本,可以很容易地实现多张图片合成GIF动画的功能,并带来更丰富的游戏体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值