- 浏览: 43933 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
dl96200:
辛苦了。。感谢感谢 啊
dom读取xml与写入xml -
kaven1989:
太好了,楼主辛苦了
dom读取xml与写入xml -
osacar:
效果如何?性能?
java 生成缩略图
前台页面index.jsp
wed.xml
ImageServlet.java
ImageScale.java图片生成缩略图关键类
<%@ 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 'index.jsp' starting page</title> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <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"> <title>绝对定位+jquery.animate方法打造低成本炫彩相册</title> <link rel="stylesheet" type="text/css" href="css/style2.css" /> <link rel="stylesheet" type="text/css" href="css/lightbox.css" media="screen" /> <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/longGallery.js" type="text/javascript"></script> <script src="js/jquery.lightbox-min.js" type="text/javascript"></script> <script> function startOnload(){ /* var arrys=new Array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg','9.jpg','10.jpg','11.jpg','12.jpg','13.jpg','ce2c4-large.jpg'); var docs=document.getElementById("ul_gallery"); //var docs=document.createElement("ul"); for(i=0;i<arrys.length;i++){ var mydiv=document.createElement("li"); mydiv.setAttribute("id","itemdiv"); mydiv.innerHTML="<a href='H:/image/item/"+arrys[i]+" '><img src='H:/image/item/"+arrys[i]+"' alt='' /></a>"; docs.appendChild(mydiv); } */ endOnload(); } </script> </head> <body onload="startOnload()"> <br><br> 图片缩放测试 <br/> <img src="imageServlet" /> <img src="imageServlet?size=2" /> <img src="imageServlet?size=4" /> <img src="imageServlet?size=6" /> <div id="wrapper"> <div id="galleryBox"> <p id="p_bot"><span id="span_prev">«</span><span id="span_auto"></span><span id="span_next">»</span></p> <ul id="ul_gallery"> <li><a href="images\11.jpg"><img src="images\11.jpg" alt="" /></a></li> <li><a href="imageServlet?size=2"><img src="imageServlet?size=2" alt="" /></a></li> <li><a href="imageServlet?size=4"><img src="imageServlet?size=4" alt="" /></a></li> <li><a href="imageServlet?size=6"><img src="imageServlet?size=6" alt="" /></a></li> </ul> <span id="this_prev">»</span><span id="this_next">«</span> </div> </div> </body> </html>
wed.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>org.img.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/imageServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
ImageServlet.java
package org.img; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ImageServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置页面的头信息 response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); // 图片放在当前应用的images目录下 如 http://www.xxx.com/ 下的文件目录。 String path = getServletContext().getRealPath("images/22ww.jpg"); //构建一个带缓存的图片读取流 BufferedImage image1 = ImageIO.read(new File(path)); //获得从页面传来的值,此参数是用来设置缩略图的大小 String size=request.getParameter("size"); if(size==null){ //源图的比例传入页面 ImageIO.write(image1, "JPEG", response.getOutputStream()); }else{ try{ //获得图片的源始分辨率 int[] leng=getImageWidthAndHeight(path); //页面参数转型 int result=Integer.parseInt(size); //构建一个设置缩略图对象 ImageScale is = new ImageScale(); //设置统缩略图大小 BufferedImage image2 = is.imageZoomOut(image1,leng[0]/result,leng[1]/result); //缩略图传入页面 ImageIO.write(image2, "JPEG", response.getOutputStream()); }catch(Exception e){ e.printStackTrace(); } } } //得到图片的源始分辨率 public int[] getImageWidthAndHeight(String path) throws Exception{ int[] leng=new int[2]; InputStream is=new FileInputStream(path); BufferedImage src = javax.imageio.ImageIO.read(is); //构造Image对象 int srcWidth = src.getWidth(null); //得到源图宽 int srcHeight = src.getHeight(null); //得到源图长 leng[0]=srcWidth; leng[1]=srcHeight; // System.out.print(srcWidth+"-"+srcHeight); return leng; } }
ImageScale.java图片生成缩略图关键类
package org.img; import java.awt.image.BufferedImage; public class ImageScale { private int width; private int height; private int scaleWidth; double support = (double) 3.0; double PI = (double) 3.14159265358978; double[] contrib; double[] normContrib; double[] tmpContrib; int startContrib, stopContrib; int nDots; int nHalfDots; public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) { width = srcBufferImage.getWidth(); height = srcBufferImage.getHeight(); scaleWidth = w; if (DetermineResultSize(w, h) == 1) { return srcBufferImage; } CalContrib(); BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w); BufferedImage pbFinalOut = VerticalFiltering(pbOut, h); return pbFinalOut; } /** * 决定图像尺寸 */ private int DetermineResultSize(int w, int h) { double scaleH, scaleV; scaleH = (double) w / (double) width; scaleV = (double) h / (double) height; // 需要判断一下scaleH,scaleV,不做放大操作 if (scaleH >= 1.0 && scaleV >= 1.0) { return 1; } return 0; } // end of DetermineResultSize() private double Lanczos(int i, int inWidth, int outWidth, double Support) { double x; x = (double) i * (double) outWidth / (double) inWidth; return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support) / (x * PI / Support); } // end of Lanczos() // // Assumption: same horizontal and vertical scaling factor // private void CalContrib() { nHalfDots = (int) ((double) width * support / (double) scaleWidth); nDots = nHalfDots * 2 + 1; try { contrib = new double[nDots]; normContrib = new double[nDots]; tmpContrib = new double[nDots]; } catch (Exception e) { System.out.println("init contrib,normContrib,tmpContrib" + e); } int center = nHalfDots; contrib[center] = 1.0; double weight = 0.0; int i = 0; for (i = 1; i <= center; i++) { contrib[center + i] = Lanczos(i, width, scaleWidth, support); weight += contrib[center + i]; } for (i = center - 1; i >= 0; i--) { contrib[i] = contrib[center * 2 - i]; } weight = weight * 2 + 1.0; for (i = 0; i <= center; i++) { normContrib[i] = contrib[i] / weight; } for (i = center + 1; i < nDots; i++) { normContrib[i] = normContrib[center * 2 - i]; } } // end of CalContrib() // 处理边缘 private void CalTempContrib(int start, int stop) { double weight = 0; int i = 0; for (i = start; i <= stop; i++) { weight += contrib[i]; } for (i = start; i <= stop; i++) { tmpContrib[i] = contrib[i] / weight; } } // end of CalTempContrib() private int GetRedValue(int rgbValue) { int temp = rgbValue & 0x00ff0000; return temp >> 16; } private int GetGreenValue(int rgbValue) { int temp = rgbValue & 0x0000ff00; return temp >> 8; } private int GetBlueValue(int rgbValue) { return rgbValue & 0x000000ff; } private int ComRGB(int redValue, int greenValue, int blueValue) { return (redValue << 16) + (greenValue << 8) + blueValue; } // 行水平滤波 private int HorizontalFilter(BufferedImage bufImg, int startX, int stopX, int start, int stop, int y, double[] pContrib) { double valueRed = 0.0; double valueGreen = 0.0; double valueBlue = 0.0; int valueRGB = 0; int i, j; for (i = startX, j = start; i <= stopX; i++, j++) { valueRGB = bufImg.getRGB(i, y); valueRed += GetRedValue(valueRGB) * pContrib[j]; valueGreen += GetGreenValue(valueRGB) * pContrib[j]; valueBlue += GetBlueValue(valueRGB) * pContrib[j]; } valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen), Clip((int) valueBlue)); return valueRGB; } // end of HorizontalFilter() // 图片水平滤波 private BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) { int dwInW = bufImage.getWidth(); int dwInH = bufImage.getHeight(); int value = 0; BufferedImage pbOut = new BufferedImage(iOutW, dwInH, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < iOutW; x++) { int startX; int start; int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5); int y = 0; startX = X - nHalfDots; if (startX < 0) { startX = 0; start = nHalfDots - X; } else { start = 0; } int stop; int stopX = X + nHalfDots; if (stopX > (dwInW - 1)) { stopX = dwInW - 1; stop = nHalfDots + (dwInW - 1 - X); } else { stop = nHalfDots * 2; } if (start > 0 || stop < nDots - 1) { CalTempContrib(start, stop); for (y = 0; y < dwInH; y++) { value = HorizontalFilter(bufImage, startX, stopX, start, stop, y, tmpContrib); pbOut.setRGB(x, y, value); } } else { for (y = 0; y < dwInH; y++) { value = HorizontalFilter(bufImage, startX, stopX, start, stop, y, normContrib); pbOut.setRGB(x, y, value); } } } return pbOut; } // end of HorizontalFiltering() private int VerticalFilter(BufferedImage pbInImage, int startY, int stopY, int start, int stop, int x, double[] pContrib) { double valueRed = 0.0; double valueGreen = 0.0; double valueBlue = 0.0; int valueRGB = 0; int i, j; for (i = startY, j = start; i <= stopY; i++, j++) { valueRGB = pbInImage.getRGB(x, i); valueRed += GetRedValue(valueRGB) * pContrib[j]; valueGreen += GetGreenValue(valueRGB) * pContrib[j]; valueBlue += GetBlueValue(valueRGB) * pContrib[j]; // System.out.println(valueRed+"->"+Clip((int)valueRed)+"<-"); // // System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-"); // System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->"); } valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen), Clip((int) valueBlue)); // System.out.println(valueRGB); return valueRGB; } // end of VerticalFilter() private BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) { int iW = pbImage.getWidth(); int iH = pbImage.getHeight(); int value = 0; BufferedImage pbOut = new BufferedImage(iW, iOutH, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < iOutH; y++) { int startY; int start; int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5); startY = Y - nHalfDots; if (startY < 0) { startY = 0; start = nHalfDots - Y; } else { start = 0; } int stop; int stopY = Y + nHalfDots; if (stopY > (int) (iH - 1)) { stopY = iH - 1; stop = nHalfDots + (iH - 1 - Y); } else { stop = nHalfDots * 2; } if (start > 0 || stop < nDots - 1) { CalTempContrib(start, stop); for (int x = 0; x < iW; x++) { value = VerticalFilter(pbImage, startY, stopY, start, stop, x, tmpContrib); pbOut.setRGB(x, y, value); } } else { for (int x = 0; x < iW; x++) { value = VerticalFilter(pbImage, startY, stopY, start, stop, x, normContrib); pbOut.setRGB(x, y, value); } } } return pbOut; } // end of VerticalFiltering() int Clip(int x) { if (x < 0) return 0; if (x > 255) return 255; return x; } }
- image.rar (807.2 KB)
- 下载次数: 60
相关推荐
在本文中,我们将介绍使用Java生成缩略图的方法。 缩略图生成方法 在 Java 中,生成缩略图可以使用多种方法,本文将介绍两种常见的方法。 方法一:使用BufferedImage类 使用BufferedImage类可以实现图片的缩略图...
根据提供的文件信息,我们可以总结出以下关于“Java生成缩略图”的相关知识点: ### Java生成缩略图基础知识 #### 缩略图的概念 缩略图是原始图像的一个较小版本,通常用于网页展示、图片预览等场景。通过生成缩略...
java 生成缩略图类 源代码 (已经封装好)
### Java 上传图片生成缩略图的知识点解析 在现代Web开发中,处理图像是一项常见的需求,尤其是在涉及用户上传图片的应用场景中。本篇文章将基于提供的代码片段详细讲解如何使用Java来实现上传图片并自动生成缩略图...
java图片裁剪和java生成缩略图.pdf
Java 生成缩略图的方法示例 Java 生成缩略图是指使用 Java 语言生成缩略图的过程,这个过程中涉及到各种图形处理技巧。下面将详细介绍 Java 生成缩略图的方法示例。 图像处理基础 在 Java 中,图像处理是通过使用...
在这个"java生成缩略图.zip"压缩包中,我们可能找到了一份Java源代码,用于演示如何在Java环境下创建图像的缩略图。 首先,我们要了解在Java中生成缩略图的关键概念和技术。Java的`java.awt.image`和`javax.imageio...
将图片生成缩略图和转换为二进制的工具类,缩略图大小可调
在Java中生成缩略图是一项常见的任务,尤其是在开发Web应用或者需要处理用户上传图片的系统中。这个过程涉及对原始图像进行重新尺寸调整,以创建一个较小的版本,通常用于预览或节省存储空间。在提供的代码片段中,...
java 生成缩略图 java 生成缩略图 java 生成缩略图java 生成缩略图java 生成缩略图
使用 phantomjs 和 im4java 生成网页缩略图,切图用的是ImageMagick,附送im4java调用ImageMagick处理图片代码,包括剪切、调整大小、调整到指定比例大小不足部分补白边。包含 phantomjs 包,不包含ImageMagick。
在这个特定的场景中,"JSP上传图片并生成缩略图"是一个常见的功能需求,尤其是在开发包含用户交互和多媒体内容的Web应用时。下面我们将详细探讨这一技术实现的关键知识点。 首先,**上传组件**是Web应用中用于接收...
在Java编程中,生成缩略图是一项常见的任务,特别是在开发Web应用或处理用户上传的图像时。这个过程涉及读取原始图像,调整其尺寸,并保存为新的格式,以适应特定的展示需求。以下是一个关于如何在Java中创建缩略图...
6. **Java bindings for FFmpeg:** 如果不希望通过命令行接口,还可以使用Java对FFmpeg的绑定库,如JAVE(Java Audio Video Encoder),它提供了一个友好的API来执行FFmpeg的任务,包括生成缩略图。 7. **处理步骤...
生成缩略图通常可以借助于图像处理库,如Java的`javax.imageio.ImageIO`或第三方库如Apache Commons Imaging。以下是一个简单的示例: ```java try { BufferedImage originalImage = ImageIO.read(file....
在Java编程环境中,获取视频缩略图是一项常见的需求,尤其在多媒体应用或者视频分享平台中。这个过程涉及到多媒体处理和图像操作的知识。本文将详细解释如何在Java中实现这一功能,以及相关的技术点。 首先,Java...
/** * 压缩图片方法 * * @param oldFile 将要压缩的图片 * @param width 压缩宽 * @param height 压缩高 * @param quality 压缩清晰度 建议为1.0 * @param smallIcon 压缩图片后,添加的扩展名(在图片后缀名...
5. **生成缩略图**:利用Java API或其他第三方库(如Thumbnails)来生成图片的缩略版本。 6. **将图片数据存储到数据库**:将处理后的图片数据(包括原图和缩略图)存储到数据库中。 #### 二、代码详解与知识点分析...
在Java编程环境中,批量上传图片并生成缩略图是一项常见的需求,特别是在开发Web应用时,例如内容管理系统或者论坛。这个任务通常涉及到文件处理、图像处理和服务器端编程等多个技术领域。接下来,我们将深入探讨...