[size=large][b]方法一[/b][/size]
网上下了解析gif的代码,有以下几个类(可以在网上找到):
AnimatedGifEncoder
GifDecoder
LZWEncoder
NeuQuant
调用的例子:
问题:
解析出来的图片颜色的怪怪的,背景是红色的...反正和原来看到的gif不一样,找半天原因,可能和 全局色表(global color table)有关,但还没弄明白。
[size=large][b]方法二[/b][/size]
网上下了解析gif的代码,有以下几个类(可以在网上找到):
AnimatedGifEncoder
GifDecoder
LZWEncoder
NeuQuant
调用的例子:
public static void main(String[] args) throws ImageFormatException,
IOException {
GifDecoder d = new GifDecoder();
d.read("C:\\Users\\rui\\Desktop\\1.gif");
// d.decodeImageData();
int n = d.getFrameCount();
System.out.println(d.gctFlag);
System.out.println(d.gctSize);
System.out.println(d.gct);
// for(int i : d.gct) System.out.println(i);
for (int i = 0; i < n; i++) {
BufferedImage frame = d.getFrame(i); // frame i
int t = d.getDelay(i);
FileOutputStream fos = new FileOutputStream("C:\\Users\\rui\\Desktop\\zrzr\\" + i + ".jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(frame);
fos.close();
}
}
问题:
解析出来的图片颜色的怪怪的,背景是红色的...反正和原来看到的gif不一样,找半天原因,可能和 全局色表(global color table)有关,但还没弄明白。
[size=large][b]方法二[/b][/size]
File file = new File("C:\\Users\\Administrator\\Desktop\\1.gif");
FileImageInputStream in = new FileImageInputStream(file);
//GIFImageReader reader = null;
ImageReaderSpi readerSpi = new GIFImageReaderSpi();
//readerSpi.canDecodeInput(in);
GIFImageReader gifReader = (GIFImageReader) readerSpi.createReaderInstance();
gifReader.setInput(in);
int num = gifReader.getNumImages(true);
gifReader.getNumImages(true);
System.out.println(num);
ImageWriterSpi writerSpi = new GIFImageWriterSpi();
GIFImageWriter writer = (GIFImageWriter) writerSpi.createWriterInstance();
for (int i = 0; i < num; i++) {
File newfile = new File("C:\\Users\\Administrator\\Desktop\\zrzr\\"+i+".jpg");
FileImageOutputStream out = new FileImageOutputStream(newfile);
writer.setOutput(out);
writer.write(gifReader.read(i));
out.close();
}