|
 |
|
|
10-06-2006, 10:27 AM
|
Image resizing in Java?
|
Posts: 107
Name: Rick Palmer
|
I have a J2EE application that will receive images from a file upload page, and I need to be able to resize the images into a standard size when I display them in another page (e.g. height=125 and width=125).
I know of some PHP methods to do this, but what are some ways to do it in Java?
Thanks for the help,
__________________
Please login or register to view this content. Registration is FREE - free online training in Java, J2EE, and MySQL.
Please login or register to view this content. Registration is FREE - answers and advice from a geek who knows stuff.
|
|
|
|
10-07-2006, 06:59 AM
|
Re: Image resizing in Java?
|
Posts: 43,949
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
10-30-2006, 12:05 PM
|
Re: Image resizing in Java?
|
Posts: 107
Name: Rick Palmer
|
I ended up using the built-in javax.imageid.ImageIO object to handle the scaling.
Here's my processing function:
Code:
public static InputStream scaleImage(InputStream p_image, int p_width, int p_height) throws Exception {
InputStream imageStream = new BufferedInputStream(p_image);
Image image = (Image) ImageIO.read(imageStream);
int thumbWidth = p_width;
int thumbHeight = p_height;
// Make sure the aspect ratio is maintained, so the image is not skewed
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
// Draw the scaled image
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
// Write the scaled image to the outputstream
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.
getDefaultJPEGEncodeParam(thumbImage);
int quality = 100; // Use between 1 and 100, with 100 being highest quality
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
ImageIO.write(thumbImage, IMAGE_JPG , out);
// Read the outputstream into the inputstream for the return value
ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
return bis;
}
I'm using Struts for the file upload part (where the user submits the photo image from a web page). A FormFile has a getInputStream method that is used to pass the photo's inputstream to the scaleImage() mothod above.
Assuming that you've set up a Struts form with a photo property, then you'd use the following code in the StrutsAction to access the uploaded photo's inputstream:
Code:
FormFile image = strutsForm.getPhoto();
if (image != null && image.getFileSize() > 0) {
// This returns an inputstream which you can save to your database or write to a file
ImageUtil.scaleImage(strutsForm.getPhoto().getInputStream(), 500, 500)));
}
__________________
Please login or register to view this content. Registration is FREE - free online training in Java, J2EE, and MySQL.
Please login or register to view this content. Registration is FREE - answers and advice from a geek who knows stuff.
|
|
|
|
12-07-2006, 01:50 AM
|
Re: Image resizing in Java?
|
Posts: 1
Name: Mike
|
Hello,
This code works perfect!!
I have only one question. If I want to save my file to the database I first use newCV.setBinaryContent(fileItem.getInputStream(),f ileItem.getSize()); to set the binary content where fileItem is the file returned from my Form. For the resize I use you code, but then I need to know the size after the resize. How do I get that.
Thx
Mike
|
|
|
|
01-15-2007, 06:36 AM
|
Re: Image resizing in Java?
|
Posts: 7
Name: MK
Location: Hong Kong
|
Yes there are a few Java based library like JAI and the sample code from RickPl is a fine example. But, I find the quality of resizing images is bad with large image.
So I suggest you can try calling unix pipe with ImageMagick to do image resizing. Check out the code here. You will find the quality by ImageMagick would be a lot better! But it is slightly more difficult to setup the Imagemagick properly.... good luck
__________________
Please login or register to view this content. Registration is FREE online flower shop
|
|
|
|
01-18-2007, 10:09 PM
|
Re: Image resizing in Java?
|
Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
|
Quote:
Originally Posted by duyve028
I have only one question. If I want to save my file to the database I first use newCV.setBinaryContent(fileItem.getInputStream(),f ileItem.getSize()); to set the binary content where fileItem is the file returned from my Form. For the resize I use you code, but then I need to know the size after the resize. How do I get that.
|
I'm not sure how to do what you're asking with PHP + MySQL. But, as a suggestion, you might consider saving the resized image to the file system, and then only storing the filename in the database.
With SQL Server you'll get far better performance this way, moreso the more images you add. A database engine is typically optimized for smaller, more structured types of data, and lots of BLOB data can interfere with the rdbms cache.
|
|
|
|
01-18-2007, 10:20 PM
|
Re: Image resizing in Java?
|
Posts: 28
|
hi rick : thanks your code.It help us alot ^_^
__________________
Please login or register to view this content. Registration is FREE ==>PHP Tutorials,PHP Example,PHP Script,Online Project,PHP News,....and more,helper coding...
Please login or register to view this content. Registration is FREE ==>Dog Information
|
|
|
|
03-07-2007, 12:08 PM
|
Re: Image resizing in Java?
|
Posts: 107
Name: Rick Palmer
|
Glad to help! 
__________________
Please login or register to view this content. Registration is FREE - free online training in Java, J2EE, and MySQL.
Please login or register to view this content. Registration is FREE - answers and advice from a geek who knows stuff.
|
|
|
|
03-07-2007, 01:56 PM
|
Re: Image resizing in Java?
|
Posts: 5,662
Name: John Alexander
|
Don't use bilinear though, I read bicubic is much better.
|
|
|
|
09-05-2007, 05:43 AM
|
Re: Image resizing in Java?
|
Posts: 1
|
hi,
the code given by Rick works for images whose height and width are greater than p_height and p_width respectively. When an image whose height and width is less than p_height and p_width, problem occurs.
I tried to check the height and width, and if the the original image's height and width are less than p_height and p_width, then just return the fileInputStream. but there is some problem.
I am downloading and displaying the image after scaling and uploading, but the is some problem with smaller images as i said .
|
|
|
|
04-29-2008, 04:36 PM
|
Re: Image resizing in Java?
|
Posts: 1
|
|
|
|
|
|
« Reply to Image resizing in Java?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|