You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "C.F. Scheidecker Antunes" <na...@antunes.eti.br> on 2005/08/19 03:44:18 UTC

Resizing output image from a Struts Action

Hello all,

I have an action to retrieve an image from a database BLOB field and 
display it.
It works great.

However I wanted to resize the image proportionaly. Hence I think on 
writing a second action, that would
call the first in order to retrieve the image. Then, would work on this 
image to resize it and send it over to the browser.
Or maybe change the original action to do that to save the overhead.

I would like to know how to accomplish image resizing in Java. The image 
can be a JPG, a GIF or a BMP. Hence I need to handle
all these situations depending on the Content/Type that is stored in the 
database along with the BLOB.

When you retrieve a BLOB you write it to a ByteArrayOutputStream. Then 
you just set the response.SetContentType , setContentLength and send the 
ByteArrayOutputStream by calling the method .toByteArray() and .flush.

I have two options, I can change the original Action to accept a request 
attribute to determine the width or height. Then I can work the image 
and spit it out. The way I think about it is that I would need to, 
somehow, create an Image object by taking the ByteArray from the 
ByteArrayOutputStream. This image object could be a JPG, GIF or BMP. I 
would determine which one to use based on the Content/Type of the 
database. They this object could be resized and return a byteArray that 
I could send to the browser.

I guess I have most of it done but I would like advise on how to:

1) Construct a JPG, GIF or BMP object with the ByteArray
2) Resize this object and produce a resided ByteArray so that I can send 
it to the browser.

Any ideas on how to perform that?

Any documentation regarding this that I could browse?

Thanks,

C.F.

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


[OT] Re: Resizing output image from a Struts Action

Posted by James Neville <ja...@babcockbrown.com>.
CF,

This probably isn't the best list to be asking these types of questions, 
but seeing as you wrote such a long mail, i figured a pointer is at 
least warranted ;)

I don't think you can resize an image directly from a ByteArray, but 
someone here will probably prove me wrong.
We need similar functionality in one of our apps, but we also store the 
resized preview back to the DB for later use.
We can also pass the width/height in the form, but we make sure it fits 
a max size rect.

1. Write the image to a File from the ByteArrayOutputStream.
2. Use Java Advanced Imaging to resize it.
3. Use another ByteArrayOutputStream to return it to the browser, 
setting content type etc, just as you did from the BLOB.

Theres more about this in the mailing list archives, and sample code on 
the Sun JAI site.

Regards,
James.

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


Re: Resizing output image from a Struts Action

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "C.F. Scheidecker Antunes" <na...@antunes.eti.br>

> I guess I have most of it done but I would like advise on how to:
>
> 1) Construct a JPG, GIF or BMP object with the ByteArray
> 2) Resize this object and produce a resided ByteArray so that I can send 
> it to the browser.

I've used Marco Schmidt's ImageInfo  http://schmidt.devlib.org/image-info/ 
in the past to retrieve height/width information from JPG files in order to 
calculate the size for a "max 600 pixels on the long side" smaller version. 
I'll look for the code at home if you want to see it.

Also see Roedy's glossary page on images: 
http://mindprod.com/jgloss/image.html.

-- 
Wendy Smoak


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