Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216922
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-14 15:13:33

转载自

#代码省去了冗长的异常捕获处理。
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
import com.gif4j.GifEncoder;

public class PhotoHandle{
    public static void main(String[] args)throws Exception{
    BufferedImage img = ImageIO.read(new File("1.jpg"));  
                    int h = img.getHeight();  
                    int w = img.getWidth();  
                      
                    if(h>=96 && w >=96){  
                        int nw = 96;  
                        int nh = (nw * h) / w;  
                        if(nh>96) {  
                            nh = 96;  
                            nw = (nh * w) / h;  
                        }  
                        ByteArrayOutputStream out = new ByteArrayOutputStream();  
                        BufferedImage dest = new BufferedImage(nw, nh,BufferedImage.TYPE_4BYTE_ABGR);  
                        dest.getGraphics().drawImage(img,0,0,nw, nh,null);  
                        GifEncoder.encode(dest, out);  
                        //ImageIO.write(dest, "gif", out);  
                        FileOutputStream fos=new FileOutputStream("2.jpg");
                        out.writeTo(fos);
                        out.close();
                        fos.close();
                    }  
                  }
              }

    另外还要使用到gif4j.jar包,可以在中找到。
   

阅读(982) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~