Checking Image height and width during upload

Experts,

I am using Struts to upload an image. I am able to access the FormFile object and store it into the database.

However, before uploading the image, I would like to examine the image being uploaded to make sure that it is a certain dimension. If it is not, I would like to issue an error message asking the user to re-upload a correct image. How would I do this?

Taking this a step further, would it be possible to automatically resize the image when inserting it into the database? Either solution is fine. Could someone please give me an example of how this can be accomplished.

Thanks
gvijay1Asked:
Who is Participating?
 
aozarovCommented:
>> I am using WSAD 5.1 and when I try to use the ImageIO object, I am getting some classloader error.
What is the error?
You can add the ImageIO libraries (if your JVM doesn't have them) from here: http://java.sun.com/products/java-media/jai/downloads/download-iio-1_0_01.html

Yes, you can do that but it is not that simple.
1. you need to convert the inputStream to byte array using ByteArrayOutputSream
2. Create an ImageIcom using the byte array from 1
3. use this link to create BufferedImage from Image (using the ImageIcom from 2) -> http://javaalmanac.com/egs/java.awt.image/Image2Buf.html
0
 
aozarovCommented:
FormFile has no real knowledge of the type of the file.
For scaling your will need to create an Image manipulate it and then transform it back to inputstream (so you can write it to DB).

You can create a BufferedImage via:
http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html#read(java.io.InputStream)

BufferedImage image = ImageIO.read(forFile.getInputStream());

Then you can apply the needed transformation: http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html
For more BufferedImage API methods see: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html

Last, convert the scaled image back to inputStream:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", baos); // you can replace "jpeg" with "gif", "png" or any image type handled by imageio.
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
0
 
gvijay1Author Commented:
I am using WSAD 5.1 and when I try to use the ImageIO object, I am getting some classloader error.

Is there another way to obtain the BufferedImage object?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gvijay1Author Commented:
Ok, the problem was that WSAD 5.0 runs JRE 1.3 but WSAD 5.1 runs JRE 1.4. When I used the correct JRE, the class loads fine. I saw your earlier note about transforming an image..I am getting another error this time when I run it. Here is the code that I am running.

FileInputStream in = new FileInputStream("C:/g_clemens_ft.jpeg");
BufferedImage image = ImageIO.read(in);

AffineTransform tx = new AffineTransform();
tx.scale(100, 100);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
image = op.filter(image, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
FileOutputStream outputFile = new FileOutputStream("C:/alteredImage.jpeg");
outputFile.write(baos.toByteArray());

I am getting java.lang.OutOfMemoryError when the following line executes:
image = op.filter(image, null);

0
 
aozarovCommented:
Do you run your application with enough memory? How big is your image?
Try running it with -Xmx<new_memory_size>mb
e.g: -Xmx1024m to run it with a max memory of 1G
0
 
gvijay1Author Commented:
Yes, it works now. The problem was that I was using scale(100,100) thinking that it was width/height measurements. The VM just could not handle that sort of transformation. I used parameters that were a ratio of the current size and it seems like it works now.

Thanks.
0
 
aozarovCommented:
Nice :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.