package test;
import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.image.AffineTransformOp;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
public class UploadImg ...{
String fromdir; // fromdir 图片的原始目录
String todir; // todir 处理后的图片存放目录
String imgfile; // imgfile 原始图片
String sysimgfile; // sysimgfile 处理后的图片文件名前缀
UploadImg(String fromdirNow, String todirNow, String imgfileNow,
String sysimgfileNow) ...{
fromdir = fromdirNow;
todir = todirNow;
imgfile = imgfileNow;
sysimgfile = sysimgfileNow;
}
public boolean CreateThumbnail() throws Exception ...{
// fileExtNmae是图片的格式 gif JPG 或png
// String fileExtNmae="";
double Ratio = 0.0;
File F = new File(fromdir, imgfile);
if (!F.isFile())
throw new Exception(F
+ " is not image file error in CreateThumbnail!");
File ThF = new File(todir, sysimgfile + ".jpg");
BufferedImage Bi = ImageIO.read(F);
// 假设图片宽 高 最大为300 3000
Image Itemp = Bi.getScaledInstance(300, 300, Bi.SCALE_SMOOTH);
if ((Bi.getHeight() > 300) || (Bi.getWidth() > 300)) ...{
if (Bi.getHeight() > Bi.getWidth())
Ratio = 300.0 / Bi.getHeight();
else
Ratio = 300.0 / Bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try ...{
ImageIO.write((BufferedImage) Itemp, "jpg", ThF);
} catch (Exception ex) ...{
throw new Exception(" ImageIo.write error in CreatThum.: "
+ ex.getMessage());
}
return (true);
}
public static void main(String[] args) ...{
UploadImg UI;
boolean ss = false;
try ...{
UI = new UploadImg("d:/test", "d:/test", "ae86.jpg", "ps_low1");
ss = UI.CreateThumbnail();
if (ss) ...{
System.out.println("Success");
} else ...{
System.out.println("Error");
}
} catch (Exception e) ...{
System.out.print(e.toString());
}
}
}