首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> JSP 程式設計 >> 關於JSP

JSP 生成圖片縮略圖代碼

     編輯:關於JSP

上傳完成後動態生成縮略圖並儲存。

<%@ page import="java.io.*,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*" %>
<%
  File file = new File("E:\\1110168154_58348.jpg");
  String newUrl = "E:\\1110168154_58348_min.jpg";
  Image src = ImageIO.read(file);
  int oldW = src.getWidth(null), oldH = src.getHeight(null);
  float tagSize = 287;
  float temp = (oldW > oldH) ? oldW / tagSize : oldH / tagSize;
  int newW = Math.round(oldW / temp);
  int newH = Math.round(oldH / temp);
  BufferedImage tag = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB);
  tag.getGraphics().drawImage(src, 0, 0, newW, newH, null);
  FileOutputStream fos = new FileOutputStream(newUrl);
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
  encoder.encode(tag);
  fos.close();
%>

按比例縮放,最大邊限制為 287 像素,使用舊版 com.sun.image.codec.jpeg 編碼輸出。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved