2013-05-20 66 views
14

我試圖設置一個簡單的Java程序,從多個其他圖像(JPG)創建一個單一的動畫GIF。任何人都可以給我一個如何在Java中實現這個鉤子?我已經搜索谷歌,但找不到任何真正有用的東西。有沒有辦法從Java中的多個圖像創建一個Gif圖像?

謝謝你們!

+0

你的意思是一個GIF動畫?或者你想要一個由一些小GIF製成的大GIF?或者你想用gif的透明度在另一個上粘貼一個gif? – Dariusz

+0

我想要一個動畫gif。 – user2399314

+0

這個[link](https://github.com/dragon66/icafe/wiki)比創建GIF動畫更多的信息。 – dragon66

回答

24

在這裏,你有一個創建不同圖像的gif動畫類的例子:

Link

這個類提供了這些方法:

class GifSequenceWriter { 
    public GifSequenceWriter(
     ImageOutputStream outputStream, 
     int imageType, 
     int timeBetweenFramesMS, 
     boolean loopContinuously); 

    public void writeToSequence(RenderedImage img); 

    public void close(); 
} 

,也是一個小例子:

public static void main(String[] args) throws Exception { 
    if (args.length > 1) { 
    // grab the output image type from the first image in the sequence 
    BufferedImage firstImage = ImageIO.read(new File(args[0])); 

    // create a new BufferedOutputStream with the last argument 
    ImageOutputStream output = 
     new FileImageOutputStream(new File(args[args.length - 1])); 

    // create a gif sequence with the type of the first image, 1 second 
    // between frames, which loops continuously 
    GifSequenceWriter writer = 
     new GifSequenceWriter(output, firstImage.getType(), 1, false); 

    // write out the first image to our sequence... 
    writer.writeToSequence(firstImage); 
    for(int i=1; i<args.length-1; i++) { 
     BufferedImage nextImage = ImageIO.read(new File(args[i])); 
     writer.writeToSequence(nextImage); 
    } 

    writer.close(); 
    output.close(); 
    } else { 
    System.out.println(
     "Usage: java GifSequenceWriter [list of gif files] [output file]"); 
    } 
} 
+8

回答中包含鏈接內容的主要部分,以防萬一鏈接脫機。 – Pshemo

+0

你說得對,現在編輯。 –

+0

您只包含類'GifSequenceWriter'的接口(構造函數和方法中沒有主體,所以我們不能真正使用它)。最重要的是[它是如何實現的](http://elliot.kroo.net/software/java/GifSequenceWriter/GifSequenceWriter.java)。 – Pshemo

相關問題