![]() |
|
|
Image resizing in Java? |
|
Super Talker
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,
__________________
online-etraining - free online training in Java, J2EE, and MySQL. rickysays - answers and advice from a geek who knows stuff. |
|
|
|
| Sponsored Links (We share ad revenue): |
|
|
Re: Image resizing in Java? |
|
Super Moderator
![]() Posts: 10,609
Location: Blackpool. UK
|
how about Google
__________________
Chris. ->> Links are advertising NOT optimising!! <<- Indifference will be the downfall of mankind, but who cares? Code Samples | People Counting System |
|
|
|
|
|
Re: Image resizing in Java? |
|
Super Talker
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;
}
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)));
}
__________________
online-etraining - free online training in Java, J2EE, and MySQL. rickysays - answers and advice from a geek who knows stuff. |
|
|
|
|
|
Re: Image resizing in Java? |
|
Novice Talker
Posts: 8
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
__________________
Fleur.hk online flower shop |
|
|
|
|
|
Re: Image resizing in Java? | |
|
Half Man, Half Amazing
![]()
Latest Blog Post:
North Lake Union Posts: 2,917
Name: Forrest Croce
Location: Seattle, WA
|
Quote:
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. |
|
|
|
|
|
|
Re: Image resizing in Java? |
|
Average Talker
Posts: 28
|
hi rick : thanks your code.It help us alot ^_^
__________________
T4VN.NET ==>PHP Tutorials,PHP Example,PHP Script,Online Project,PHP News,....and more,helper coding... Dog Training Site ==>Dog Information |
|
|
|
|
|
Re: Image resizing in Java? |
|
Super Talker
Posts: 107
Name: Rick Palmer
|
Glad to help!
![]()
__________________
online-etraining - free online training in Java, J2EE, and MySQL. rickysays - answers and advice from a geek who knows stuff. |
|
|
|
|
|
Re: Image resizing in Java? |
|
Moderator
![]()
Latest Blog Post:
My Favorite Isaac Asimov Story Posts: 4,068
Name: John Alexander
|
Don't use bilinear though, I read bicubic is much better.
|
|
|
|
| Sponsored Links (We share ad revenue): |
| Thread Tools | |
|
|
| Webmaster Resources Marketplace: |
| Software Development Company | Webhosting.UK.com |
| Web Templates | Text Link Brokers | Stock Photos |