写了一个实用的图像放大缩小程序,但是动画GIF转换后不会显示了,只有第一帧

写了一个实用的图像放大缩小程序,但是动画GIF转换后不会显示了,只有第一帧
代码如下,有没做过GIF转换的,提提建议一下,谢谢。 

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.util.Arrays;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
 
/**
 *一个实用的图像放大缩小程序
 *
 *@authorYuLimin
 */
publicclass ZoomImage
{
    privatestaticfinal Logger logger = Logger.getLogger(ZoomImage.class);
    privatestatic Component component = new Canvas();
    // ".pcx","tga",".tif"这三种格式目前还不支持;
    // 这些定义的格式经过我测试过是可以支持的。
    privatestatic String[] imageFormatArray = new String[]{".jpg",".jpeg",".gif",".png",".bmp"};
 
    /**
     *测试用例
     *
     *@paramargs
     *@throwsException
     */
    publicstaticvoid main(String[] args) throws Exception
    {
       ZoomImage zoomImage = new ZoomImage();
       zoomImage.listFormt();

   // 缩小四倍  

   zoomImage.zoom("E:/图片/城市风光",0.25,false);  

   // 放大四倍  

   zoomImage.zoom("E:/图片/自然风光",4,false);

    }
 
    /**
     *查看图像I/O库所支持的图像格式有哪些格式
     */ 
    publicvoid listFormt()
    {
       String readerMIMETypes[] = ImageIO.getReaderMIMETypes();
       String writerMIMETypes[] = ImageIO.getWriterMIMETypes();
       String readerFormatNames[] = ImageIO.getReaderFormatNames();
       String writerFormatNames[] = ImageIO.getWriterFormatNames();
       if(logger.isInfoEnabled())
       {
           logger.info("ReaderMIMETypes:" + Arrays.asList(readerMIMETypes));
           logger.info("WriterMIMETypes:" + Arrays.asList(writerMIMETypes));
           logger.info("ReaderFormatNames:" + Arrays.asList(readerFormatNames));
           logger.info("WriterFormatNames:" + Arrays.asList(writerFormatNames));
       }
    }
 
    /**
     *校验图像文件的格式是否可以进行缩放
     *
     *@paramfileName
     *@return
     */
    publicboolean verifyImageFormat(String fileName)
    {
       boolean result = false;
       for(int i = 0;i < imageFormatArray.length;i++)
       {
           if(fileName.toLowerCase().lastIndexOf(imageFormatArray[i]) == (fileName.length() - imageFormatArray[i].length()))
           {
              result = true;
              break;
           }
       }
       return result;
    }
 
    /**
     *将目录下的所有图像进行放大缩小
     *
     *@paramstrDir图像的目录
     *@paramzoomRatio放大缩小的倍率
     *@paramrebuild是否重新创建,即已经存在的图像是否覆盖重建
     *@throwsException
     */
    publicvoid zoom(String strDir,double zoomRatio,boolean rebuild) throws Exception
    {
       File fileDir = new File(strDir);
       if(!fileDir.exists())
       {
           logger.warn("Not exist:" + strDir);
           return;
       }
       String dirTarget = strDir + "/Zoom" + zoomRatio;
       File fileTarget = new File(dirTarget);
       if(!fileTarget.exists())
       {
           fileTarget.mkdir();
       }
       File[] files = fileDir.listFiles();
       StringBuilder stringBuilder = new StringBuilder();
       for(int i = 0;i < files.length;i++)
       {
           String fileFullName = files[i].getCanonicalPath();
           String fileShortName = files[i].getName();
           if(!new File(fileFullName).isDirectory())// 排除二级目录,如果想就再递归一次,这里省略
           {
              if(verifyImageFormat(fileShortName))
              {
                  if(logger.isInfoEnabled())
                  {
                     logger.info("Begin Zoom:" + fileFullName);
                  }
                  stringBuilder = new StringBuilder();
                  stringBuilder.append(dirTarget).append("/").append(fileShortName);
                  if(!new File(stringBuilder.toString()).exists() || rebuild)
                  {
                     try
                     {
                         createZoomSizeImage(fileFullName,stringBuilder.toString(),zoomRatio);
                     }
                     catch(Exception e)
                     {
                         logger.error("createZoomSizeImage Error:" + fileFullName,e);
                     }
                  }
                  if(logger.isInfoEnabled())
                  {
                     logger.info("End Zoom:" + fileFullName);
                  }
              }
              else
              {
                  logger.warn("Can't Zoom:" + fileFullName);
              }
           }
       }
    }
 
    /**
     *按比例进行放大缩小图像,zoomRatio=1为原大,zoomRatio>1为放大,zoomRatio< 1 为缩小
     *
     *@paramfileName
     *@paramfileNameTarget
     *@paramzoomRatio
     *@throwsException
     */
    publicvoid createZoomSizeImage(String fileName,String fileNameTarget,double zoomRatio) throws Exception
    {
       Image image = ImageIO.read(new File(fileName));
       int width = new Double(image.getWidth(null) * zoomRatio).intValue();
       int height = new Double(image.getHeight(null) * zoomRatio).intValue();
       AreaAveragingScaleFilter areaAveragingScaleFilter = new AreaAveragingScaleFilter(width,height);
       FilteredImageSource filteredImageSource = new FilteredImageSource(image.getSource(),areaAveragingScaleFilter);
       BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
       Graphics graphics = bufferedImage.createGraphics();
       graphics.drawImage(component.createImage(filteredImageSource),0,0,null);
       ImageIO.write(bufferedImage,"JPEG",new File(fileNameTarget));
    }
}
 
上一篇:线程池


下一篇:线程池的使用ExecutorService