程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp中生成圖片縮略圖的代碼

jsp中生成圖片縮略圖的代碼

編輯:關於JSP

<%@ 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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved