javaBean 自动生成缩略图(原创)

 

package com.GYW.common;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import com.GYW.model.NEWSPic;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;


public class ImgCast {
   
   
public static void Cast(HttpServletRequest request,NEWSPic pic) {
      
try {
          String outPath 
= request.getRealPath("/")+"castedImgFolder\\";
          String readPath
=request.getRealPath("/")+"uploadPic\\";
          File fi 
= new File(readPath+pic.getPicUrl());   //大图文件
          File fo = new File(outPath+pic.getPicUrl());  //将要转换出的小图文件
          int nw = 145//根据宽度比例
          AffineTransform transform = new AffineTransform();
          BufferedImage bis 
= ImageIO.read(fi);
           
int w = bis.getWidth();
          
int h = bis.getHeight();
          
double scale = (double)w/h;
          
int nh = (nw*h)/w ;
          
double sx = (double)nw/w;
          
double sy = (double)nh/h;
          transform.setToScale(sx,sy);
          AffineTransformOp ato 
= new AffineTransformOp(transform,null);
          BufferedImage bid 
= new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
          ato.filter(bis,bid);
          ImageIO.write(bid,
"jpeg",fo);
      }
 catch(Exception e) {
         e.printStackTrace();
      }

   }


}


适用于各J2EE程序,自动生成缩略图,加快网页浏览速度!

posted on 2008-02-18 16:58  Squall  阅读(429)  评论(0编辑  收藏  举报

导航