【问题标题】:I need to resize images upon upload in Java / Struts. How to do this? [closed]我需要在 Java / Struts 中调整上传图像的大小。这该怎么做? [关闭]
【发布时间】:2010-01-18 20:28:35
【问题描述】:

我们正在构建一个处理图像上传的应用程序。我希望用户能够上传给定尺寸的图像,例如 128x128,然后让服务器根据该尺寸自动生成其他尺寸,例如 64x64、57x57 和 25x25。

有没有图书馆可以帮助我解决这个问题?

编辑:我应该注意,调整大小应该只在上传时进行。当新的图像尺寸在浏览器中呈现时,它应该从缓存的副本中提取——而不是再次调整大小。换句话说,不同的尺寸应该只生成一次,而不是每次请求图像时。

【问题讨论】:

    标签: java image struts resize


    【解决方案1】:

    来自a related source in the Swing tutorial

    /**
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
    

    【讨论】:

    • 保持纵横比怎么样?
    【解决方案2】:

    来自我们的 java 门户页面之一的实用程序功能 (来自几个论坛的例子,我不声称是作者)

    希望有帮助
    纪尧姆·帕特里

    /**
     * Convenience method that returns a scaled instance of the
     * provided {@code BufferedImage}.
     *
     * @param img the original image to be scaled
     * @param targetWidth the desired width of the scaled instance,
     *    in pixels
     * @param targetHeight the desired height of the scaled instance,
     *    in pixels
     * @param hint one of the rendering hints that corresponds to
     *    {@code RenderingHints.KEY_INTERPOLATION} (e.g.
     *    {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
     *    {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
     *    {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
     * @param higherQuality if true, this method will use a multi-step
     *    scaling technique that provides higher quality than the usual
     *    one-step technique (only useful in downscaling cases, where
     *    {@code targetWidth} or {@code targetHeight} is
     *    smaller than the original dimensions, and generally only when
     *    the {@code BILINEAR} hint is specified)
     * @return a scaled version of the original {@code BufferedImage}
     */
     public BufferedImage getScaledInstance(
                BufferedImage img,
                int targetWidth,
                int targetHeight,
                Object hint,
                boolean higherQuality) {
    BufferedImage ret = (BufferedImage) img;
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {  
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }
    
    do {
       if (higherQuality) {
          if (w > targetWidth) {
             w /= 2;
             if (w < targetWidth) {
                w = targetWidth;
             }
          } else {
             w = targetWidth;
          }
          if (h > targetHeight) {
             h /= 2;
             if (h < targetHeight) {
            h = targetHeight;
             }
          } else {
             h = targetHeight;
          }
       }
       BufferedImage tmp = null;
       if (img.getType() == 0) {
          tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
       } else {
          tmp = new BufferedImage(w, h, img.getType());
       }
       Graphics2D g2 = tmp.createGraphics();
       g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
       g2.drawImage(ret, 0, 0, w, h, null);
       g2.dispose();
       ret = tmp;
    } while (w != targetWidth || h != targetHeight);
    return ret;
    

    }

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 2022-11-18
      • 2014-04-28
      • 1970-01-01
      • 2014-05-21
      • 2012-07-08
      • 2010-12-10
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多