图片文件生成缩略图的方法

引言:图片新闻、各种门户网站经常需要在页面显示图片的缩略图,然后点击缩略图查看大图或其它信息,可见将根据图片文件生成缩略图是相当有用的.
代码大致如下,有比较详细的注释:

package com.ljm.action;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.mail.iap.Response;

/**
*
*/
public class ImgUtil {
private String srcFile;
private String destFile;
private int width;
private int height;
long filesize;
private Image img;
/**
*
* @param fileName:完整文件名称
* @throws IOException
*/
public ImgUtil(String fileName) throws IOException {
File _file = new File(fileName); // 读入文件
filesize = _file.length();
this.srcFile = _file.getName();
this.destFile = this.srcFile.substring(0, this.srcFile.lastIndexOf("."))+ "_s.jpg";
img = javax.imageio.ImageIO.read(_file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
System.out.println("得到源图宽" + width);
height = img.getHeight(null); // 得到源图长
System.out.println("得到源图长" + height);

}
/**
* 根据已有图片文件生成缩略图文件
* @param w:要形成的缩略图的宽度
* @param h:要形成的缩略图的高度
* @param objFilename:要生成的缩略图文件名
* @throws IOException
*/
public void resize(int w, int h, String objFilename) throws IOException {
BufferedImage _image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(objFilename); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image); // 近JPEG编码
out.close();
}
/**
* 根据已有图片文件生成缩略图直接打印到页面
* @param w:要形成的缩略图的宽度
* @param h:要形成的缩略图的高度
* @param response:HttpServletResponse对象
* @throws IOException
*/
public void resize(int w,int h,HttpServletResponse response) throws IOException {
BufferedImage _image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
OutputStream out=response.getOutputStream();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image); // 近JPEG编码
out.close();
}

public long getFilesize() {
return filesize;
}

public void setFilesize(long filesize) {
this.filesize = filesize;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}
}

3.action里调用:try {
ImgUtil imgUtil=new ImgUtil("g:\\ljm\\logo.gif");
imgUtil.resize(500, 500, "g:\\ljm\\11.gif");
imgUtil.resize(500, 500, response);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值