struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kris Schneider <k...@dotech.com>
Subject Re: not quite struts: handling Images
Date Tue, 27 Jul 2004 13:23:33 GMT
One thing to keep in mind with ImageIO and a long-running process, like an app
server, is its potential use of a disk-based cache. You may end up with lots of
temporary files that will only be removed if the VM exits normally. See:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#deleteOnExit()

There's also this interesting bug:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513817

To disable the disk-based cache in favor of a memory-based cache, use
ImageIO.setUseCache(false).

Once you have a BufferedImage, one way to resize it is with AffineTransformOp:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/AffineTransformOp.html
http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html

As for saving with custom compression, here's something I've used before:

protected void writeImage(RenderedImage image, OutputStream out) throws
IOException {
  ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
  ImageWriter imageWriter =
(ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();
  imageWriter.setOutput(imageOut);
  ImageWriteParam writeParam = imageWriter.getDefaultWriteParam();
  writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  writeParam.setCompressionType("JPEG");
  writeParam.setCompressionQuality(1);
  imageWriter.write(null, new IIOImage(image, null, null), writeParam);
}

Quoting Mark Lowe <mark.lowe@boxstuff.com>:

> Not sure about the resize and save (3-4) but the rest i use for 
> ensuring users dont upload any old sizes..
> 
> 
> java.io.ByteArrayInputStream;
> java.awt.image.BufferedImage;
> javax.imageio.ImageIO;
> 
> byte[] image = form.getImage().getFileData();
> ByteArrayInputStream  stream = new ByteArrayInputStream(image);
> BufferedImage img = ImageIO.read(stream);
> 
> BufferedImage has getWidth and getHeight
> 
> Perhaps also better not to use getFileData on the formFile, and use the 
> getInputStream method instead as its less heavy..
> 
> Mark
> 
> On 27 Jul 2004, at 10:28, ron1 wrote:
> 
> > Hi -
> > after spending much time wondring through the JAI API I thought maybe 
> > one of you guys can help me:
> > I need a very simple:
> > 1. upload-image (jpg/gif)
> > 2. getProps (size, width)
> > 3. resize
> > 4. save JPG with custom compression.
> > The upload is done via FormFile, so I have the InputStream - all the 
> > rest via Java API - and I have no clue of where to get started :-(
> > Cheers,
> > Ron

-- 
Kris Schneider <mailto:kris@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message