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