天天看点

写了一个实用的图像放大缩小程序,但是动画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是否重新创建,即已经存在的图像是否覆盖重建

    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);

                     logger.info("end zoom:" + filefullname);

              }

              else

                  logger.warn("can't zoom:" + filefullname);

     *按比例进行放大缩小图像,zoomratio=1为原大,zoomratio>1为放大,zoomratio< 1 为缩小

     *@paramfilenametarget

     *@paramzoomratio

    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));

}

继续阅读