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
小果子 閱讀(188)
評論(0) 編輯 收藏 引用 所屬分類:
學習筆記