图片生成缩略图

项目中曾经用到客户上传了很大的图片(50M),要在页面上显示出来。呵呵,后果就是页面一时半时出不来了,所以整理一下生成缩略图的类。(最后也没用到,让客户自己把图片ps小了)

 

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 生成缩略图
 * @author liugang
 *
 */
public class ImgUtil {
 private int old_w = 0; // 源图宽
 private int old_h = 0; // 源图高
 private int new_w = 0; //缩略图宽
 private int new_h = 0; //缩略图高
 private String oldImagPath=""; //原图地址
 private String newImgPath=""; //缩略图地址
 private String errMsg="";//错误提示
 
 /**
  * 根据输入的图像宽、高生成缩略图。宽、高可以输入一个,当另一个为0时自动按比例计算
  * @param oldPath 原图地址
  * @param newPath 缩略图地址
  * @param w   生成的图片的宽
  * @param h   生成的图片的高
  */
 public boolean createImg(String oldPath,String newPath,int w,int h){
  this.oldImagPath=oldPath;
  this.newImgPath=newPath;  
  //读取文件
  Image img=this.readFile();
  if(img==null){
   return false;
  }
  //如果存在宽或高一个为0的情况按比例计算
  if(w==0||h==0){
   double rate=0; //比例
   if(w==0&&h!=0){
    rate=h/(double)old_h;
    this.new_h=h;
    this.new_w=(int)Math.round(old_w * rate);    
   }else if(h==0&&w!=0){
    rate=w/(double)old_w;
    this.new_w=w;
    this.new_h=(int)Math.round(old_h * rate);
   }
  }else{
   this.new_h=h;
   this.new_w=w;
  }
  //生成缩略图
  boolean b=this.createMinImg(img);
  
  return b;
 }
 /**
  * 根据百分比生成缩略图
 * @param oldPath 原图地址
  * @param newPath 缩略图地址
  * @param rate 百分比
  * @return
  */
 public boolean createImg(String oldPath,String newPath,int rate){
  this.oldImagPath=oldPath;
  this.newImgPath=newPath;
  
  //读取文件
  Image img=this.readFile();
  if(img==null){
   return false;
  }
  //根据百分比计算宽、高  
  this.new_h=Math.round(old_h * rate / 100);
  this.new_w=Math.round(old_w * rate / 100);
  
  //生成缩略图
  boolean b=this.createMinImg(img);
  
  return b;
 }
 /**
  * 读取原图文件,并生成起宽、高
  * @return
  */
 private Image readFile(){
  Image src =null;
  //判断原图地址
  if(oldImagPath.equals("")||oldImagPath==null){
   this.setErrMsg("原图地址为空");
   return null;
  } 
  try {
   java.io.File file = new java.io.File(oldImagPath); // 读入文件
   src = javax.imageio.ImageIO.read(file); // 构造Image对象
   //取图像的高、宽
   old_w = src.getWidth(null);
   old_h = src.getHeight(null);
   
   //System.out.println("the old width is :" + old_w+ " the old height is " + old_h);
  }catch (NullPointerException e){
   this.setErrMsg("原图地址为空!");
  }catch (IllegalArgumentException e){
   this.setErrMsg("图像文件为空!");
  }catch (IOException e){
   this.setErrMsg("读取图像文件IO错误!");
  }
  return src;
 }
 /**
  * 生成缩略图
  */
 private boolean createMinImg(Image src) {
  //判断缩略图地址
  if(newImgPath.equals("")||newImgPath==null){
   this.setErrMsg("缩略图地址为空");
   return false;
  }
  //判断是否存在缩略图的宽、高
  if(new_w==0||new_h==0){
   this.setErrMsg("缩略图的宽:"+String.valueOf(new_w)+"高为"+String.valueOf(new_h)+",不能生成缩略图!");
   return false;
  }
  //System.out.println("the new width is :" + new_w+ " the new height is " + new_h);
  try {        
   BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);
   tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // 绘制缩小后的图
   FileOutputStream newimage = new FileOutputStream(newImgPath); // 输出到文件流
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
   encoder.encode(tag); // 近JPEG编码
   newimage.close();
  } catch (FileNotFoundException e) {
   this.setErrMsg("如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开 ");
   return false;
  }catch(SecurityException e){
   this.setErrMsg("图像文件已存在,但拒绝访问! ");
   return false;
  }catch(IOException e){
   this.setErrMsg("读取图像文件IO错误!");
   return false;
  }
  return true;
 }
 
 
 public String getErrMsg() {
  return errMsg;
 }
 public void setErrMsg(String errMsg) {
  this.errMsg = errMsg;
 }
 public int getNew_w() {
  return new_w;
 }
 public void setNew_w(int newW) {
  new_w = newW;
 }
 public int getNew_h() {
  return new_h;
 }
 public void setNew_h(int newH) {
  new_h = newH;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值