package JingJiaGuo;

import javax.imageio.*;
import java.awt.image.*;
import java.io.*;

public class Image
{
public Image(String path)

throws IOException
{
bi=ImageIO.read(new File(path));
weight=bi.getWidth();
height=bi.getHeight();
}
public void SaveTo(String path,String formatname,int maxlen,boolean flag)

throws FileNotFoundException,IOException
{
int nw=weight,nh=height;

if(flag)
{

if(weight>maxlen)
{
nw=maxlen;
nh=height*nw/weight;

if(nh>maxlen)
{
nh=maxlen;
nw=weight*nh/height;
}
}

if(height>maxlen)
{
nh=maxlen;
nw=weight*nh/height;

if(nw>maxlen)
{
nw=maxlen;
nh=height*nw/weight;
}
}

}else
{
nw=nh=maxlen;
}
int imagetype=BufferedImage.TYPE_INT_RGB;
if(formatname!="jpeg")imagetype=BufferedImage.TYPE_4BYTE_ABGR_PRE;
ByteArrayOutputStream bous=new ByteArrayOutputStream();
BufferedImage tmp=new BufferedImage(nw,nh,imagetype);
tmp.getGraphics().drawImage(bi,0,0,nw,nh,null);
ImageIO.write(tmp,formatname,bous);
FileOutputStream fos=new FileOutputStream(path);
fos.write(bous.toByteArray());
fos.close();
}
public int weight,height;
public BufferedImage bi;
}

暂时未支持动态的gif,不过好像有个开源的gif89的一个开源库,里面有支持动态的gif图像处理的,记下学习笔记
posted on 2009-05-13 22:19
小果子 阅读(187)
评论(0) 编辑 收藏 引用 所属分类:
学习笔记