• Post Reply Bookmark Topic Watch Topic
  • New Topic

converting Image size  RSS feed

 
Priya Venkatesan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement such that given any image of any size and resolution, I need to convert it into a specified size.
For (eg) the user could upload 1000 * 1000 image, but the maximum size allowed on the JSP page is maximum of 200 * 200. By doing this the size of image will also be reduced so the loading time will not be affected?

I trying to similar to what webshots or yahoo photos are doing. Is there any mechanism in Java by which we can do this.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the image is in Java it's trivially easy to do it. For example, if it's loaded as type Image then you can use getScaledInstance and specify the size and a hint for the algorithm to use. If it's loaded as a RenderedImage I would look at Java Advanced Imaging and use the Scale operation.

If you don't know how to read/write the images then look at Java's ImageIO package and also check out JAI's ImageIO stuff as well. Java supports GIF, JPEG, PNG and I think BMP. If you want to work with TIF or something else you'll need JAI. JAI may also be faster too, I'm not sure.
[ January 27, 2006: Message edited by: Ken Blair ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resizing an image to a smaller size or down-sampling is a non-trivial process since there are many algorithms to do this, some that support anti-aliasing. It's also processor intensive at times.

If this is for a website, like a webshots index page, I recommend constructing the thumbnails ahead of time using photoshop or a similar tool. Having the thumbnails constructed on the fly would use a lot of processing power.

As for libraries, I'm sure there's a number of third party and open source solution. There is the AffineTransformation API which might be of some use although I have not used it in a long time: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/geom/AffineTransform.html
 
Rashid Mayes
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the following

http://fotomica.astrientlabs.com/slideshow?c=people&li=10

and

http://fotomica.astrientlabs.com/slideshow?c=people&li=10&h=100&w=150

The scaling is perfomed in java and is controlled by the h and w parameters. The code is similar to:



Also, look at http://fotomica.astrientlabs.com/galleries.jsp

Again, all the images are handled by one servlet. There are several ways to achieve scaling and you will need to find the solution that fits your quality/performace expectations.
[ January 27, 2006: Message edited by: Rashid Mayes ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have performance metrics for code like this? I'd be concerned about the performance of it in larger systems, although one appropriate solution is to cache image sizes so that the same image is not generated repeatedly.

Still, from a web developer standpoint, I strongly prefer generation tools that generate all the thumbnails ahead of time. Developing (or borrowing) intelligent generation tools isn't much different from writing a solution that will do it on the fly.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
Resizing an image to a smaller size or down-sampling is a non-trivial process since there are many algorithms to do this, some that support anti-aliasing. It's also processor intensive at times.

If this is for a website, like a webshots index page, I recommend constructing the thumbnails ahead of time using photoshop or a similar tool. Having the thumbnails constructed on the fly would use a lot of processing power.

As for libraries, I'm sure there's a number of third party and open source solution. There is the AffineTransformation API which might be of some use although I have not used it in a long time: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/geom/AffineTransform.html


The code to do it is trivial to write using existing libraries. Based upon the question it sounds like they merely want to scale the image to the maximum size when the user uploads it. That doesn't require it be done on the fly when pages are loaded, it just requires doing it once each time a user uploads an image and will likely take a very small amount of time using JAI.
 
Rashid Mayes
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To echo the prior response, I would not be too concerned about the performance of JAI in the use described above. You can use JAI at any step in your web application. I have worked on several projects that have used JAI to scale images, much larger than 1000x1000, on uploads.

Also, keep in mind that the latest JAI includes native binaries.

For the example in the first link I listed,

http://fotomica.astrientlabs.com/slideshow?c=people&li=10

the load times are not completely reflective of how long it takes to scale. In fact, all the following takes place before the images loads.


1.Using FlickrJ, contact flickr and get a list of images that match the tag �people�
2.Using FlickrJ, get the URL for the 10th image in the list
3.Render an image using a stream connection to the URL (flickr.com).
4.Scale the resulting image.
5.Encode the scaled image as a JPEG and stream to the client (browser).

All this happens relatively quickly. Please see JAI and imageIO at http://java.sun.com/products/java-media/jai/index.jsp

Thanks.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized my link to JAI was broken, it's fixed now. They have mailing lists where the developers are pretty good about answering questions too, which is a big bonus when trying to develop applications that use it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!