Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Image resizing in Java?
Old 10-06-2006, 10:27 AM Image resizing in Java?
RickPlmr's Avatar
Super Talker

Posts: 107
Name: Rick Palmer
Trades: 0
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.
RickPlmr is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-07-2006, 06:59 AM Re: Image resizing in Java?
chrishirst's Avatar
Defies a Status

Posts: 43,949
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
how about Google
__________________
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?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-30-2006, 12:05 PM Re: Image resizing in Java?
RickPlmr's Avatar
Super Talker

Posts: 107
Name: Rick Palmer
Trades: 0
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.
RickPlmr is offline
Reply With Quote
View Public Profile
 
Old 12-07-2006, 01:50 AM Re: Image resizing in Java?
Junior Talker

Posts: 1
Name: Mike
Trades: 0
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
duyve028 is offline
Reply With Quote
View Public Profile
 
Old 01-15-2007, 06:36 AM Re: Image resizing in Java?
Novice Talker

Posts: 7
Name: MK
Location: Hong Kong
Trades: 0
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
maliciouskitty is offline
Reply With Quote
View Public Profile Visit maliciouskitty's homepage!
 
Old 01-18-2007, 10:09 PM Re: Image resizing in Java?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Quote:
Originally Posted by duyve028 View Post
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 01-18-2007, 10:20 PM Re: Image resizing in Java?
Average Talker

Posts: 28
Trades: 0
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
t4vn.net is offline
Reply With Quote
View Public Profile
 
Old 03-07-2007, 12:08 PM Re: Image resizing in Java?
RickPlmr's Avatar
Super Talker

Posts: 107
Name: Rick Palmer
Trades: 0
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.
RickPlmr is offline
Reply With Quote
View Public Profile
 
Old 03-07-2007, 01:56 PM Re: Image resizing in Java?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Don't use bilinear though, I read bicubic is much better.
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 05:43 AM Re: Image resizing in Java?
Junior Talker

Posts: 1
Trades: 0
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 .
google_code is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 04:36 PM Re: Image resizing in Java?
Junior Talker

Posts: 1
Trades: 0
http://www.comesolvego.com/2008/04/2...king-solution/
vpetreski is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Image resizing in Java?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46314 seconds with 11 queries