JSP 图片转换

来源:互联网 发布:程序员面试自我介绍 编辑:程序博客网 时间:2024/06/15 21:01

<%@page import="java.awt.*,java.awt.image.*,java.util.*,com.sun.image.codec.jpeg.*"%>
<%@ page import="java.awt.Image" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'picConvert.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    This is my JSP page. <br>
    <%
    java.io.File file = new java.io.File("C:/Users/yanggx/Desktop/新建文件夹/3.jpg");        //读入刚才上传的文件
    String newurl= "C:/afger.jpg";  //新的缩略图保存地址
    Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    float tagsize=200;
    int old_w=src.getWidth(null);                                     //得到源图宽
    int old_h=src.getHeight(null); 
    int new_w=0;
    int new_h=0;                            //得到源图长
    int temps;
    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);//计算新图长宽
    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>

0 0
原创粉丝点击