public boolean CreateThumbnail(String _toDir, String _fromDir) throws Exception
{
//存放路径
String todir = _toDir;
String fromdir = _fromDir;
double Ratio = 0.0;
File ThF = new File(todir);
InputStream input = new FileInputStream(fromdir);
BufferedImage Bi = ImageIO.read(input);//getInputStream()
//假设图片宽 高 最大为80 80
Image Itemp = Bi.getScaledInstance(80, 80, Bi.SCALE_SMOOTH);
if ( (Bi.getHeight() > 80) || (Bi.getWidth() > 80)) {
if (Bi.getHeight() > Bi.getWidth())
Ratio = 80.0 / Bi.getHeight();
else
Ratio = 80.0 / Bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
Graphics grd = Itemp.getGraphics();
//加注自己的标志字
grd.setColor(Color.red);
grd.setFont(new Font("Times New Roman",Font.PLAIN,18));
grd.drawString("YangTom",30,10);
grd.dispose();
try {
ImageIO.write( (BufferedImage) Itemp, getExt(todir), ThF);
}
catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: " +
ex.getMessage());
}finally{
if(input!=null){
try{
input.close();
}catch(Exception e){
System.out.println("Close the InputStream is fail:");
e.printStackTrace();
}
}
}
return (true);
}
//获取后缀
private String getExt(String dir){
String filename = dir;
int lastdot = filename.lastIndexOf(".");
String ext = filename.substring(lastdot+1);
return ext;
}