java JSP 如何缩放图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2938698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP How to scale an image?
提问by user236501
Is there anyway to scale an image then display in jsp page? When retrieve and display the images, I want to show all photos in same size. is there any API can do it? I have searched from google, those I found was about scaling images byusing tookit but can't works in web application.
无论如何缩放图像然后在jsp页面中显示?检索和显示图像时,我想以相同大小显示所有照片。有什么API可以做到吗?我从谷歌搜索过,我发现那些是关于使用 takeit 缩放图像的,但不能在 Web 应用程序中工作。
采纳答案by BalusC
You can use the builtin Java 2D APIfor this (basic Sun tutorial here).
您可以为此使用内置的Java 2D API(此处为基本 Sun 教程)。
Basically, you need to create a Servletwhich gets an InputStreamof the original image in the doGet()method, passes it through the Java 2D API and then writes it to the OutputStreamof the HTTP response. Then you just map this Servlet on a certain url-patternin web.xml, e.g. /thumbs/*and call this Servlet in the srcattribute of the HTML <img>element.
基本上,您需要创建一个Servlet,它InputStream在doGet()方法中获取原始图像的,通过 Java 2D API 传递它,然后将其写入OutputStreamHTTP 响应的 。然后你只需将这个 Servlet 映射到某个url-patternin 上web.xml,例如,在 HTML元素/thumbs/*的src属性中调用这个 Servlet <img>。
Here's a basic kickoff example (you still need to handle unexpected conditions yourself the way you want):
这是一个基本的启动示例(您仍然需要按照自己的方式自己处理意外情况):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// First get image file name as request pathinfo (or parameter, whatever you want).
String imageFilename = request.getPathInfo().substring(1);
// And get the thumbnail dimensions as request parameters as well.
int thumbWidth = Integer.parseInt(request.getParameter("w"));
int thumbHeight = Integer.parseInt(request.getParameter("h"));
// Then get an InputStream of image from for example local disk file system.
InputStream imageInput = new FileInputStream(new File("/images", imageFilename));
// Now scale the image using Java 2D API to the desired thumb size.
Image image = ImageIO.read(imageInput);
BufferedImage thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumb.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
// Write the image as JPG to the response along with correct content type.
response.setContentType("image/jpeg");
ImageIO.write(thumb, "JPG", response.getOutputStream());
}
With the servlet mapped in web.xmlas follows:
servlet 映射web.xml如下:
<servlet>
<servlet-name>thumbServlet</servlet-name>
<servlet-class>com.example.ThumbServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>thumbServlet</servlet-name>
<url-pattern>/thumbs/*</url-pattern>
</servlet-mapping>
This can be used as follows:
这可以按如下方式使用:
<img src="thumbs/filename.jpg?w=100&h=100" width="100" height="100">
Note: no, this cannot be done with JSP alone since it's a view technology unsuitable for this task.
注意:不,这不能单独使用 JSP 来完成,因为它是一种不适合此任务的视图技术。
Note 2: this is a pretty expensive (CPU intensive) task, keep this in mind. You may want to consider to cache or pregenerate the thumbs beforehand yourself.
注意 2:这是一项非常昂贵(CPU 密集型)的任务,请记住这一点。您可能需要考虑自己预先缓存或预生成拇指。
回答by jwenting
Note: no, this cannot be done with JSP alone since it's a view technology unsuitable for this task.
注意:不,这不能单独使用 JSP 来完成,因为它是一种不适合此任务的视图技术。
Technically you can do it, but indeed it's not something that's advisable.
从技术上讲,您可以这样做,但实际上这不是可取的。
And yes, I'd cache the images for a long time, together with something to identify changes to the original, then recreate the resized image only if the original changes (or the cache expires, maybe a week after the image was last accessed).
是的,我会将图像缓存很长时间,以及一些可以识别原始更改的内容,然后仅当原始更改(或缓存过期,可能在上次访问图像后一周)时才重新创建调整大小的图像.

