Java,J2EE,Weblogic,Oracle

java项目随笔
随笔 - 90, 文章 - 6, 评论 - 61, 引用 - 0
数据加载中……

JAVA 大图片转换为小图片

/**
*大图片文件名,生成小图片的文件名,图片宽度,图片高度
*
*/

protected boolean createMinImage(File fromImg, File toImg, int toWidth,
   int toHeight) throws Exception {
  try {
   // fileExtNmae是图片的格式 gif JPG 或png
   // String fileExtNmae="";
   double wRatio = 0.0;
   double hRatio = 0.0;
   double iRatio = 0.0;

   BufferedImage Bi = ImageIO.read(fromImg);
   // 假设图片宽 高 最大为120 120

   hRatio = ((double) toHeight) / Bi.getHeight();
   wRatio = ((double) toWidth) / Bi.getWidth();
   iRatio = wRatio < hRatio ? wRatio : hRatio;

   java.awt.Image Itemp = Bi.getScaledInstance(
     (int) (toWidth * iRatio), (int) (toHeight * iRatio),
     BufferedImage.SCALE_SMOOTH);
   AffineTransformOp op = new AffineTransformOp(AffineTransform
     .getScaleInstance(iRatio, iRatio), null);
   Itemp = op.filter(Bi, null);
   ImageIO.write((BufferedImage) Itemp, "jpg", toImg);
  } catch (Exception ex) {
   return false;
  }
  return (true);
 }

posted on 2009-05-19 15:29 龚椿深 阅读(588) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: