【问题标题】:Encoding transparent animated gif in Java在 Java 中编码透明动画 gif
【发布时间】:2012-06-12 14:00:17
【问题描述】:

我使用GifDecoder 读取动画.gif 文件并使用AnimGifEncoder 编写它。 (link)

如果我显示GifDecoder 读取的原始帧,它们会正确显示并且是透明的,但如果我显示AnimatedGifEncoder 创建的帧,则透明度完全错误。

   GifDecoder gif = new GifDecoder();
   gif.read("image.gif");

   AnimatedGifEncoder e = new AnimatedGifEncoder();
   e.start("newimage.gif");
   e.setTransparent(Color.BLACK);

   for (int i=0;i<gif.getFrameCount();i++) {
        anim.addFrame(gif.getFrame(i));
        anim.setDelay(gif.getDelay(i));
   }

   anim.finish();

在本例中,我将透明颜色设置为黑色。但实际上我想从GifDecoder 获取透明颜色信息,但我不知道如何。

【问题讨论】:

  • GIFanim 是否按照您的预期对图像进行编码?如果是这样,我可能会忍不住拿出代码并对其进行总结以寻求答案。

标签: java image transparency gif animated-gif


【解决方案1】:

这是很久以前的事了,但当时我确实设法让它工作了。无需再次挖掘代码......我得到了它的工作:

  1. 扫描原始图像并将透明区域设置为原始图像中不存在的颜色。

  2. 将 AnimGifEncoder 中的透明颜色设置为之前分配给透明区域的颜色。

【讨论】:

    【解决方案2】:

    我正在使用以下解决方案,尽管仍有一些 gif 文件 缩放后一团糟……:

    (至少它似乎与大多数不透明的 gif 相处得很好)

    Color transparentColor = null;
    BufferedImage firstFrameImage = ImageIO.read([sourceFileHere]);
    
    if (firstFrameImage.getColorModel() instanceof IndexColorModel) {
       IndexColorModel cm = (IndexColorModel) firstFrameImage.getColorModel();
       int transparentPixel = cm.getTransparentPixel();
       transparentColor = new Color(cm.getRGB(transparentPixel), true);
    }
    
    e.setTransparent(transparentColor);
    

    【讨论】:

      猜你喜欢
      • 2016-08-09
      • 2014-06-10
      • 2012-08-26
      • 2019-08-28
      • 2023-03-03
      • 2021-03-19
      • 2015-12-21
      • 2011-09-11
      • 2013-02-15
      相关资源
      最近更新 更多