图片处理类

public class ImgHandle{

private Picture oldpicture;
private Picture newpicture;

private ImageFilter imageFilter=new ImageFilter();
private FileDeal fd=new FileDeal();

public void ImgHandle(){}


public void copyImg(Picture oldpicture,Picture newpicture) throws Exception
{
String newimgname="";
try{
File originfile=new File(oldpicture.getImgDir(),oldpicture.getImgfile());
if (!originfile.isFile()) {
throw new Exception(" there is no this image file:"+originfile.toString());
}

File targetfile=new File(newpicture.getImgDir(),newpicture.getImgfile());

fd.copy(originfile.toString(), targetfile.toString());

}catch (Exception ex) {
throw new Exception(" dealUploadImg error: "+ex.getMessage());
}

}


/**
* create a preview small picture
* it has same function as dealUploadImg()
* gif --> png
* jpg --> jpg
* */
public void CreateThumbnail(Picture oldpicture,Picture newpicture,int maxwidth,int maxheight) throws Exception
{
String ext="";
if (imageFilter.isGif(oldpicture.getImgfile())){
newpicture.setImgfile(newpicture.getImgPrefix()+".png");
ext="png";
}else if (imageFilter.isJpg(oldpicture.getImgfile())){
ext="jpg";
}else if (imageFilter.isPng(oldpicture.getImgfile())){
ext="png";
}

try {
File F = new File(oldpicture.getImgDir(),oldpicture.getImgfile());
if (!F.isFile())
return;

getImagSize(oldpicture);
double Ratio=getRadio(oldpicture,maxwidth,maxheight);

BufferedImage Bi = ImageIO.read(F);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Image Itemp = op.filter(Bi, null);

File ThF = new File(newpicture.getImgDir(),newpicture.getImgfile());
ImageIO.write((BufferedImage)Itemp, ext, ThF);

}catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage());
}

}

public double getRadio(Picture picture,int maxwidth,int maxheight){
double Ratio=1.0;

if (picture.getWidth()>picture.getHeight()){
Ratio = (new Integer(maxwidth)).doubleValue()/picture.getWidth();
}else if (picture.getWidth()<picture.getHeight()){
Ratio=(new Integer(maxheight)).doubleValue()/picture.getHeight();
}else if (picture.getWidth()==picture.getHeight()){
if (picture.getWidth()>maxwidth){
Ratio = (new Integer(maxwidth)).doubleValue()/picture.getWidth();
}

}
return Ratio;
}

public void getImagSize(Picture picture) throws Exception{

double Ratio=1.0;

try{
File F = new File(picture.getImgDir(),picture.getImgfile());
if (!F.isFile())
throw new Exception(F+" is not image file error !");

BufferedImage Bi = ImageIO.read(F);
// Image Itemp = Bi.getScaledInstance (maxwidth,maxheight,Bi.SCALE_SMOOTH);

picture.setWidth(Bi.getWidth());
picture.setHeight(Bi.getHeight());
}catch (Exception ex) {
throw new Exception(" getImagSize: "+ex.getMessage());
}

}

public List picList(String picDir,int maxwidth,int maxheight) throws Exception{
List list=new ArrayList();
try{
File directory=new File(picDir);
String[] images = directory.list(new ImageFilter());
String fromdir=picDir;
for (int i = 0; i < images.length; i++){
String imgfile=images[i];
Picture picture=new Picture(fromdir,imgfile);
getImagSize(picture);
list.add(picture);
}
return list;
}catch (Exception ex) {
throw new Exception("picList error:"+ex.getMessage());
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值