首页 > 编程 > JSP > 正文

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

2024-09-05 00:19:49
字体:
来源:转载
供稿:网友

<%@ page contenttype="text/html;charset=gb2312" %>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title> </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="wang shiqiang" />
  <meta name="keywords" content="wang shiqiang" />
  <meta name="description" content="" />
</head>

<body>
 <%
    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("<br/>the old width is :"+old_w+" the old height is "+old_h+"<br/>");
    float tempdouble;
    if(old_w>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+"<br/>");
    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();  
    /*
    */
%>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表