Checking Image height and width during upload

Posted on 2005-05-02
Medium Priority
Last Modified: 2012-08-13

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.

Question by:gvijay1
  • 4
  • 3
LVL 15

Expert Comment

ID: 13915293
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:

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());

Author Comment

ID: 13923570
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?
LVL 15

Accepted Solution

aozarov earned 400 total points
ID: 13923594
>> 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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 13926665
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");

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

LVL 15

Expert Comment

ID: 13928951
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

Author Comment

ID: 13931441
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.

LVL 15

Expert Comment

ID: 13931448
Nice :-)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question