• Java图片或视频生成GIF动图,发送微信


    前言

    别人的博客文章中有动态显示这是怎么做到的呢?别人的微信发送的表情动态为什么是自己鬼畜视频?这些都是别人做到的,本文就是让自己也可以做到以上的事情,制作鬼畜GIF动态,辣就学起来吧!

    请添加图片描述

    GIF简介

    GIF的发明者是美国计算机科学家、GIF图像格式发明人斯蒂芬•威尔海特(Stephen Wilhite)。

    GIF图形交换格式是一种位图图形文件格式,以8位色(即256种颜色)重现真彩色的图像。它实际上是一种压缩文档,采用LZW压缩算法进行编码,有效地减少了图像文件在网络上传输的时间。它是目前广泛应用于网络传输的图像格式之一。

    代码生成

    以下介绍几种生成方式。

    图片合成GIF

    如何根据给定的多张图片来合成一张gif图片。这种需求又如何来实现呢?

    
    <dependency>
      <groupId>com.madgaggroupId>
      <artifactId>animated-gif-libartifactId>
      <version>1.4version>
    dependency>
    
    <dependency>
        <groupId>cn.hutoolgroupId>
        <artifactId>hutool-allartifactId>
        <version>5.0.6version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    代码:

      public static void main(String[] args) throws Exception {
            BufferedImage image1 = ImageIO.read(new File("D:/gif/11.png"));
            BufferedImage image2 = ImageIO.read(new File("D:/gif/22.jpg"));
            BufferedImage image3 = ImageIO.read(new File("D:/gif/33.jpg"));
            BufferedImage image4 = ImageIO.read(new File("D:/gif/44.jpg"));
            AnimatedGifEncoder e = new AnimatedGifEncoder();
            //生成的图片路径
            e.start(new FileOutputStream("D:/gif.gif"));
            //图片宽高
            e.setSize(300, 190);
            //图片之间间隔时间
            e.setDelay(400);
            //重复次数 0表示无限重复 默认不重复
            e.setRepeat(0);
            //添加图片
            e.addFrame(image1);
            e.addFrame(image2);
            e.addFrame(image3);
            e.addFrame(image4);
            e.finish();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    效果如下:
    请添加图片描述

    自定义GIF动图

    自定义生成,直接使用系统创建的方式来生成gif,不使用外部的图片、视频等资源,直接在界面上绘制一个GIF图。
    代码:

     public static void main(String[] args) throws IOException {
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.start("D:/gif.gif");
            encoder.setTransparent(Color.WHITE);
            encoder.setRepeat(0);
            encoder.setDelay(50);
    
            BufferedImage img = new BufferedImage(200, 180, BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g2d = img.createGraphics();
    
            for (int i = 0; i < 100; i++) {
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, 200, 180);
                g2d.setColor(Color.BLUE);
                g2d.drawOval(0, i, 120, 120);
                encoder.addFrame(img);
            }
            g2d.dispose();
            encoder.finish();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    效果:
    请添加图片描述

    请添加图片描述

    视频生成GIF

    ws.schild是基于ffmpeg的java工具包,是目前主流的对视频和音频进行转码、裁剪以及提取操作的java工具包。

    依赖:

    <dependency>
    		<groupId>ws.schildgroupId>
    		<artifactId>jave-coreartifactId>
    		<version>3.1.1version>
    	dependency>
       
        
    	<dependency>
    		<groupId>ws.schildgroupId>
    		<artifactId>jave-nativebin-win64artifactId>
    		<version>3.1.1version>
    	dependency>
        
    	<dependency>
    		<groupId>ws.schildgroupId>
    		<artifactId>jave-nativebin-linux64artifactId>
    		<version>3.1.1version>
    	dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    <dependency>
          <groupId>ws.schildgroupId>
          <artifactId>jave-all-depsartifactId>
          <version>3.0.1version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    代码:

    import ws.schild.jave.Encoder;
    import ws.schild.jave.EncoderException;
    import ws.schild.jave.MultimediaObject;
    import ws.schild.jave.encode.EncodingAttributes;
    import ws.schild.jave.encode.VideoAttributes;
    import ws.schild.jave.info.MultimediaInfo;
    import ws.schild.jave.info.VideoInfo;
    import ws.schild.jave.info.VideoSize;
    
    import java.io.File;
    import java.util.Arrays;
    
    public class VideoToGIf {
    
        //输出格式
        private static final String outputFormat = "gif";
    
        /**
         * 获得转化后的文件名
         *
         * @param sourceFilePath : 源视频文件路径
         * @return
         */
        public static String getNewFileName(String sourceFilePath) {
            File source = new File(sourceFilePath);
            String fileName = source.getName().substring(0, source.getName().lastIndexOf("."));
            return fileName + "." + outputFormat;
        }
    
        /**
         * 转化音频格式
         *
         * @param sourceFilePath : 源视频文件路径
         * @param targetFilePath : 目标gif文件路径
         * @return
         */
        public static void transform(String sourceFilePath, String targetFilePath) {
            File source = new File(sourceFilePath);
            File target = new File(targetFilePath);
            try {
                //获得原视频的分辨率
                MultimediaObject mediaObject = new MultimediaObject(source);
                MultimediaInfo multimediaInfo = mediaObject.getInfo();
                VideoInfo videoInfo = multimediaInfo.getVideo();
                VideoSize sourceSize = videoInfo.getSize();
                //设置视频属性
                VideoAttributes video = new VideoAttributes();
                video.setCodec(outputFormat);
                //设置视频帧率 正常为10 ,值越大越流畅
                video.setFrameRate(10);
                //设置视频分辨率
                VideoSize targetSize = new VideoSize(sourceSize.getWidth() / 5, sourceSize.getHeight() / 5);
                video.setSize(targetSize);
                //设置转码属性
                EncodingAttributes attrs = new EncodingAttributes();
                attrs.setVideoAttributes(video);
                // 音频转换格式类
                Encoder encoder = new Encoder();
                encoder.encode(mediaObject, target, attrs);
                System.out.println("转换完成...");
            } catch (EncoderException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 批量转化视频格式
         *
         * @param sourceFolderPath : 源视频文件夹路径
         * @param targetFolderPath : 目标gif文件夹路径
         * @return
         */
        public static void batchTransform(String sourceFolderPath, String targetFolderPath) {
            File sourceFolder = new File(sourceFolderPath);
            if (sourceFolder.list().length != 0) {
                Arrays.asList(sourceFolder.list()).forEach(e -> {
                    transform(sourceFolderPath + "\\" + e, targetFolderPath + "\\" + getNewFileName(e));
                });
            }
        }
    
        public static void main(String[] args) {
            batchTransform("D:\\video", "D:\\video");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    效果:
    请添加图片描述

    发送微信

    在这里插入图片描述
    问题:

    发送的gif动图变成文件形式怎么办?

    解决:
    文件太大和动图尺寸过大,超过了微信的尺寸范围,那么就会以文件的形式来发送你的gif动图。
    所以当你的gif动图过大时,最好能裁剪一下尺寸或者是压缩一下大小,这样就能解决这个问题了。

    小结

    本文介绍了三种生成GIF动图的方式,自定义、图片转GIF、视频转GIF。赶快实践一下吧

    在这里插入图片描述
    点赞 收藏 关注
    吾辈读书,只有两事,一者进德之事,讲求乎诚正修齐之道,以图无忝所生,一者修业之事,操习乎记诵词章之术,以图自卫其身。

  • 相关阅读:
    使用go pprof进行golang程序内存分析
    深度分页,我都是这么玩的
    django搭建博客十博客首页
    消息积压了如何处理?
    如何用python使用redis模块来跟redis实现交互
    2022年12月 && Faster RCNN训练自己的数据集 && 配置环境相对简洁
    蛋白结构合理性评估软件-PROCHECK的安装与使用
    机器学习笔记:Huber Loss & smooth L1 loss
    Redis的高可用集群 完
    一篇文章让你熟悉unordered_set及其模拟实现
  • 原文地址:https://blog.csdn.net/qq_35764295/article/details/127549747