You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ron1 <ro...@gmx.net> on 2004/07/27 10:28:55 UTC

not quite struts: handling Images

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


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


Re: not quite struts: handling Images

Posted by Koon Yue Lam <ki...@gmail.com>.
Hi all !
I have similar problem to ron1 and these mails really help !
However if I want to extract the exif metadata of JPEG, can it be done
in Java without third party component?

Regards

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


Re: not quite struts: handling Images

Posted by Kris Schneider <kr...@dotech.com>.
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 <ma...@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 <ma...@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


Re: not quite struts: handling Images

Posted by Mark Lowe <ma...@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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org


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