SV: sizing down an uploaded photo

Mathias Höggren <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Message-ID <[email protected]>
Hi,

Quick and simple solution to the problem. The attached code scales down
images that are wider then the value stored in "MAX_IMAGE_WIDTH". To use the
method just pass your image as a BufferedImage to the method:

BufferedImage myScaledImg = scaleImage(ImageIO.read(new
File("/path/to/image.jpg")));

/* CODE: start */
private static final int MAX_IMAGE_WIDTH = 120;

private BufferedImage scaleImage(BufferedImage srcImage)
{
  //Get original size
  int imageWidth = srcImage.getWidth();
  int imageHeight = srcImage.getHeight();

  //Calculate scalingfactor
  double scale = (double)MAX_IMAGE_WIDTH / (double)srcImage.getWidth();

  //Now create thumbnail
  AffineTransform affineTransform = AffineTransform.getScaleInstance(scale,
scale);
  AffineTransformOp affineTransformOp = new
AffineTransformOp(affineTransform, null);
  BufferedImage scaledBufferedImage = affineTransformOp.filter(srcImage,
null);

  //Somewhere in AffineTransform there is a rounding error that
  //causes a thin line in the bottom of the scaled image.
  //The following code is adjusting this annoying "feature"
  int scaledWidth = scaledBufferedImage.getWidth();
  int scaledHeight = scaledBufferedImage.getHeight();
  int expectedWidth = (int)(imageWidth * scale);
  int expectedHeight = (int)(imageHeight * scale);
  if (scaledWidth > expectedWidth || scaledHeight > expectedHeight)
  {
    return scaledBufferedImage.getSubimage(0, 0, expectedWidth,
expectedHeight);
  }
  else
  {
    return scaledBufferedImage;
  }
}
/* CODE: end */

Brgds
Mathias


-----Ursprungligt meddelande-----
Från: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[email protected]]För Carter, Scott
Skickat: den 7 maj 2003 00:03
Till: [email protected]
Ämne: sizing down an uploaded photo


I am hoping that someone using servlets has encountered this problem.  I
have a web site where user's can upload photos, but after the servlet
uploads the file I would like it to also resize the deminsions of the
picture and the quality (size).  Has anyone run into this problem?

Thanks - Scott

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.