//http://www.fmsware.com/stuff/gif.zip
//先下程序中需要用到的包
//class GifDD
import image.AnimatedGifEncoder;
import image.GifDecoder;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class GifDD {
public static String[] delay; // 延迟时间
public Map<String, String> map = new HashMap<String, String>();
/**
* 把gif图片按帧拆分成jpg图片
*
* @param gifName
* String 原图路径
*
* @return BufferedImage[] 返回生成的文件流
*/
private BufferedImage[] splitGif(String gifName) {
try {
GifDecoder decoder = new GifDecoder();
decoder.read(gifName);
// decoder.read(BufferedInputStream);
// decoder.read(InputStream);
int n = decoder.getFrameCount(); // 得到frame的个数
BufferedImage[] subPic = new BufferedImage[n]; //
delay = new String[n]; // 存储延迟时间
for (int i = 0; i < n; i++) {
BufferedImage frame = decoder.getFrame(i); // 得到帧
delay[i] = decoder.getDelay(i) + ""; // 得到延迟时间
// 生成小的JPG文件
subPic[i] = frame;
}
return subPic;
} catch (Exception e) {
System.out.println("splitGif Failed!");
e.printStackTrace();
return null;
}
}
/**
* 把多张jpg图片合成一张
*
* @param bi
* BufferedImage[] 多个文件流文件名
* @param newPic
* String 生成的gif文件名 包含路径
* @param _delay
* String[] 播放的时间
*/
private void jpgToGif(BufferedImage bi[], String newPic, String _delay[]) {
try {
AnimatedGifEncoder e = new AnimatedGifEncoder(); // 网上可以找到此类
e.setRepeat(0);
e.start(newPic);
for (int i = 0; i < bi.length; i++) {
// src[i] = ImageIO.read(new File(pic[i])); // 读入需要播放的jpg文件
// e.addFrame(src[i]); // 添加到帧中
e.setDelay(new Integer(_delay[i]));// 设置播放的延迟时间
e.addFrame(bi[i]);// 添加到帧中
}
e.finish();
} catch (Exception e) {
System.out.println("jpgToGif Failed:");
e.printStackTrace();
}
}
// 处理图片
/**
* 把多张jpg图片进行处理
*
* @param bi
* BufferedImage[] 多个文件流文件名
* @param width
* String 缩放的宽度
* @param height
* String 缩放的高度
* @return BufferedImage[] 返回生成的文件流
*/
public BufferedImage[] changeImage(BufferedImage[] bi, double width,
double height) {
BufferedImage src = null;
// 存width , height
int saveWidth = 0;
int saveHeight = 0;
BufferedImage[] backBi = new BufferedImage[bi.length];
for (int i = 0; i < bi.length; i++) {
double __width = new Double(bi[i].getWidth() + ""); // src width
double __height = new Double(bi[i].getHeight() + ""); // src
// height
if (bi[i].getHeight() <= width && bi[i].getWidth() <= height) {
saveWidth = new Double(width).intValue();
saveHeight = new Double(height).intValue();
}
else if ((width / height) >= (__width / __height)) {
saveWidth = new Double(__width / __height * height).intValue();
saveHeight = new Double(height).intValue();
} else {
saveHeight = new Double((__height / __width) * width)
.intValue();
saveWidth = new Double(width).intValue();
}
BufferedImage tag = new BufferedImage(saveWidth, saveHeight,
BufferedImage.TYPE_INT_RGB);
// 绘制缩小后的图
tag.getGraphics().drawImage(bi[i], 0, 0, saveWidth, saveHeight,
null); //
backBi[i] = tag;
}
// 绘制缩小后的图
return backBi;
}
/**
* 把多张jpg图片进行存盘
*
* @param bi
* BufferedImage[] 多个文件流文件名
* @param savePath
* String 存放的路径
*
*/
public void saveImage(BufferedImage[] bi, String savePath) {
FileOutputStream out = null;
for (int i = 0; i < bi.length; i++) {
String path = savePath + "\\" + i + ".jpg";
try {
out = new FileOutputStream(path);
ImageIO.write(bi[i], "jpeg", out);// 存盘
// JPEGImageEncoder encoder =
// JPEGCodec.createJPEGEncoder(savePath);
//
// encoder.encode(bi[i]);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
BufferedImage[] bi = new GifDD().splitGif("e:\\a.gif");
new GifDD().saveImage(bi, "e:\\");
BufferedImage[] backbi = new GifDD().changeImage(bi, 32, 32);
new GifDD().saveImage(backbi, "e:\\images");
new GifDD().jpgToGif(backbi, "e:\\_a.gif", delay);
}
}
posted @
2008-02-21 14:02 anly 阅读(750) |
评论 (0) |
编辑 收藏