package JingJiaGuo;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Image
{
public Image(String path)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws FileNotFoundException,IOException
{
int nw=weight,nh=height;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(flag)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(weight>maxlen)
{
nw=maxlen;
nh=height*nw/weight;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(nh>maxlen)
{
nh=maxlen;
nw=weight*nh/height;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(height>maxlen)
{
nh=maxlen;
nw=weight*nh/height;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(nw>maxlen)
{
nw=maxlen;
nh=height*nw/weight;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}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;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
暂时未支持动态的gif,不过好像有个开源的gif89的一个开源库,里面有支持动态的gif图像处理的,记下学习笔记
posted on 2009-05-13 22:19
小果子 阅读(179)
评论(0) 编辑 收藏 引用 所属分类:
学习笔记