2012-06-08 132 views
1

我使用GifDecoder來讀取動畫的.gif文件和AnimGifEncoder來編寫它。 (linkJava中的透明GIF動畫編碼

如果我顯示由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獲得透明顏色信息,但我不知道如何。

+0

[GIFanim](http://pscode.org/gifanim/)是否按照預期對圖像進行編碼?如果是這樣,我可能會試圖拖出代碼並對其進行總結以作答案。 –

回答

1

我用以下解決方案,儘管仍有一些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);