JSP如何缩放图像? [英] JSP How to scale an image?

查看:113
本文介绍了JSP如何缩放图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何缩放图像然后在jsp页面中显示?检索并显示图像时,我想显示所有相同尺寸的照片。有什么API可以做到吗?我从谷歌搜索过,我发现这些是关于使用takeit缩放图像但在网络应用程序中无效。

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.

推荐答案

你可以使用内置的 Java 2D API (基本的Sun)教程此处)。

You can use the builtin Java 2D API for this (basic Sun tutorial here).

基本上,你需要创建一个 Servlet code> doGet()方法中的原始图像的InputStream ,将其传递给Java 2D API,然后将其写入HTTP响应的 OutputStream 。然后,您只需将此Servlet映射到 web.xml 中的某个 url-pattern ,例如 / thumbs / * 并在HTML < img>的 src 属性中调用此Servlet ; 元素。

Basically, you need to create a Servlet which gets an InputStream of the original image in the doGet() method, passes it through the Java 2D API and then writes it to the OutputStream of the HTTP response. Then you just map this Servlet on a certain url-pattern in web.xml, e.g. /thumbs/* and call this Servlet in the src attribute of the HTML <img> element.

这是一个基本启动示例(您仍然需要自己按照自己的方式处理意外情况):

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());
}

将servlet映射到 web.xml 如下:

With the servlet mapped in web.xml as follows:

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

可以按如下方式使用:

<img src="thumbs/filename.jpg?w=100&h=100" width="100" height="100">






注意:不,这不能用JSP来完成因为它是一种不适合这项任务的视图技术。


Note: no, this cannot be done with JSP alone since it's a view technology unsuitable for this task.

注2:这是一项非常昂贵的(CPU密集型)任务记住这一点。您可能需要考虑事先自己缓存或预生成拇指。

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.

这篇关于JSP如何缩放图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆