Last active
April 22, 2018 11:58
-
-
Save threepipes/19d764e35decad6789722b32b58bb1bd to your computer and use it in GitHub Desktop.
(主にTopCoderMMで)アニメーションgifを書き出すためのクラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.imageio.*; | |
| import javax.imageio.stream.ImageOutputStream; | |
| import java.awt.image.BufferedImage; | |
| import java.io.File; | |
| import java.io.IOException; | |
| public class GifWriter { | |
| ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next(); | |
| BufferedImage buffer; | |
| boolean finish = false; | |
| /** | |
| * @param filename gifを書き出すファイルパスを指定 | |
| */ | |
| GifWriter(String filename) { | |
| File outfile = new File(filename); | |
| try { | |
| ImageOutputStream ios = ImageIO.createImageOutputStream(outfile); | |
| iw.setOutput(ios); | |
| iw.prepareWriteSequence(null); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * @param buffer paintにて書き込みが行われるBufferedImageを保持 | |
| */ | |
| void set(BufferedImage buffer) { | |
| this.buffer = buffer; | |
| } | |
| /** | |
| * 書き込みが行われた画像をgifに追加 | |
| */ | |
| void next() { | |
| if(finish) return; | |
| try { | |
| Thread.sleep(20); | |
| iw.writeToSequence(new IIOImage(buffer, null, null), null); | |
| } catch (IOException | InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * 書き込み終了 | |
| */ | |
| void close() { | |
| if(finish) return; | |
| try { | |
| finish = true; | |
| iw.endWriteSequence(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使い方(走り書きなので後で例コード追加などしっかり書き直します)
下はprincessesAndMonstersでの例