jsp中生成图片缩略图的代码

2007-01-23 10:35:42  作者

%26lt;%@ page contentType="text/html;charset=gb2312" %%26gt;
%26lt;%@ page language="java" import="java.util.*" %%26gt;
%26lt;%@ page language="java" import="java.io.*" %%26gt;
%26lt;%@ page language="java" import="java.awt.*" %%26gt;
%26lt;%@ page language="java" import="java.awt.image.*" %%26gt;
%26lt;%@ page language="java" import="com.sun.image.codec.jpeg.*" %%26gt;
%26lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"%26gt;
%26lt;html xmlns="http://www.w3.org/1999/xhtml"%26gt;
%26lt;head%26gt;
%26lt;title%26gt; %26lt;/title%26gt;
%26lt;meta name="generator" content="editplus" /%26gt;
%26lt;meta name="author" content="Wang Shiqiang" /%26gt;
%26lt;meta name="keywords" content="Wang Shiqiang" /%26gt;
%26lt;meta name="description" content="" /%26gt;
%26lt;/head%26gt;

%26lt;body%26gt;
%26lt;%
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");

out.print("==================Test for thumbtail picture=============");

//-----------------------上传完成,开始生成缩略图-------------------------
java.io.File file = new java.io.File("E:\\1110168154_58348.jpg");//saveurl); //读入刚才上传的文件
String newurl="E:\\1110168154_58348_min.jpg";//request.getRealPath("/")+url+filename+"_min."+ext; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=287;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null);
int new_w=0;
int new_h=0; //得到源图长
int tempsize;
out.print("%26lt;br/%26gt;the old width is :"+old_w+" the old height is "+old_h+"%26lt;br/%26gt;");
float tempdouble;
if(old_w%26gt;old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//计算新图长宽
out.print("the new width is :"+new_w+" the new height is "+new_h+"%26lt;br/%26gt;");
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(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
/*
*/
%%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;

相关文章