【问题标题】:JSP How to scale an image?JSP如何缩放图像?
【发布时间】:2011-02-25 16:36:08
【问题描述】:

无论如何缩放图像然后显示在jsp页面中?检索和显示图像时,我想以相同大小显示所有照片。有什么API可以做到吗?我从谷歌搜索,我发现那些是关于使用 takeit 缩放图像但不能在 Web 应用程序中工作。

【问题讨论】:

    标签: java jsp servlets image-scaling


    【解决方案1】:

    您可以为此使用内置的Java 2D API(Sun 基础教程here)。

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

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

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

    web.xml 中映射的servlet 如下:

    <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 完成,因为它是一种不适合此任务的视图技术。


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

    【讨论】:

    • 我也是这样做的,图片边框只显示在excel中。任何想法?仅供参考在我指定 w 和 h 参数之前,图像正在显示但没有指定大小。
    【解决方案2】:

    注意:不,这不能用 JSP 完成 独自一人,因为它是一种视图技术 不适合这个任务。

    技术上你可以做到,但实际上这不是可取的。

    是的,我会将图像缓存很长时间,以及识别原始更改的某些内容,然后仅在原始更改(或缓存过期,可能在图像发布一周后)重新创建调整大小的图像最后访问)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      相关资源
      最近更新 更多