posts - 1,comments - 0,trackbacks - 0
          //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; // 延遲時(shí)間

              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的個(gè)數(shù)

                      BufferedImage[] subPic = new BufferedImage[n]; //

                      delay = new String[n]; // 存儲(chǔ)延遲時(shí)間

                      for (int i = 0; i < n; i++) {

                          BufferedImage frame = decoder.getFrame(i); // 得到幀

                          delay[i] = decoder.getDelay(i) + ""; // 得到延遲時(shí)間

                          // 生成小的JPG文件
                          subPic[i] = frame;

                      }
                      return subPic;
                  } catch (Exception e) {
                      System.out.println("splitGif Failed!");
                      e.printStackTrace();
                      return null;
                  }
              }

              /**
               * 把多張jpg圖片合成一張
               *
               * @param bi
               *            BufferedImage[] 多個(gè)文件流文件名
               * @param newPic
               *            String 生成的gif文件名 包含路徑
               * @param _delay
               *            String[] 播放的時(shí)間
               */

              private void jpgToGif(BufferedImage bi[], String newPic, String _delay[]) {
                  try {

                      AnimatedGifEncoder e = new AnimatedGifEncoder(); // 網(wǎng)上可以找到此類

                      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]));// 設(shè)置播放的延遲時(shí)間

                          e.addFrame(bi[i]);// 添加到幀中

                      }
                      e.finish();
                  } catch (Exception e) {
                      System.out.println("jpgToGif Failed:");
                      e.printStackTrace();
                  }
              }

              // 處理圖片

              /**
               * 把多張jpg圖片進(jìn)行處理
               *
               * @param bi
               *            BufferedImage[] 多個(gè)文件流文件名
               * @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圖片進(jìn)行存盤
               *
               * @param bi
               *            BufferedImage[] 多個(gè)文件流文件名
               * @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 閱讀(752) | 評(píng)論 (0)編輯 收藏
          主站蜘蛛池模板: 宜兰市| 桐庐县| 连平县| 岢岚县| 景谷| 通州区| 息烽县| 呼伦贝尔市| 衢州市| 庄河市| 宁陵县| 西青区| 五寨县| 门源| 红桥区| 柯坪县| 广河县| 名山县| 江达县| 屯留县| 张家港市| 肇州县| 承德市| 吉木萨尔县| 改则县| 衡南县| 响水县| 武胜县| 郯城县| 浦城县| 西乡县| 凉城县| 巧家县| 四平市| 石家庄市| 赣州市| 鄂州市| 茶陵县| 永德县| 盐津县| 阿勒泰市|