Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Image Thumbnailer  RSS feed

 
Ma New
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to create a very simple image thumbnailer in JSP such as:

Get imageX.png
Reduce 65 pixels X 65 pixels
Output imageXthumb.png

I have read through a million different documents and have seen a million different ways to do this. I know that I need it to be able to read jpg, gif, and png files, which apparently means I need to user the javax.imageIO.* . I know most people would suggest doing this with a servlet but my boss says I have to do it with JSP so if there is anyone out there who can direct to a good resource or who has some code I can look at I would really appreciate it.

I have checked:

http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/imageio_guideTOC.fm.html

I have tried to piece together a little JSP app from this info but I cant seem to get problems with the bufferedImage not being recognized in my netbeans. I then looked for other code online and found this which doesn't work either:

<%@ page import="java.io.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="javax.imageio.*" %>
<%@ page import="javax.imageio.IIOImage" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="javax.imageio.ImageIO.*" %>
<%@ page import="javax.imageio.ImageIO.getReaderFormatNames"%>
<%@ page import="javax.imageio.ImageIO.getImageReadersBySuffix"%>

<%
ImageWriter writer;
BufferedImage im = new BufferedImage("/image.jpg");

int tw = im.getWidth() / 5;
int th = im.getHeight() / 5;
BufferedImage thumb = new BufferedImage(tw, th);
Graphics2D g2D = thumb.createGraphics();
g2D.drawImage(im, 0, 0, tw, th, null);

List thumbnails = new ArrayList(1);
thumbnails.add(thumb);

// IoImage holds image, thumbnail, metadata

IIOImage iioim = new IIOImage(im, thumbnails, null);
writer.write(iioim);
%>

If some one could please help me I would be extremely appreciative
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ma New:

I have tried to piece together a little JSP app from this info but I cant seem to get problems with the bufferedImage not being recognized in my netbeans. I then looked for other code online and found this which doesn't work either:


You need to Tell The Details. When you say "it doesn't work", exactly what do you mean? Do you get a compile error? Run time exception? Unexpected results?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
7 of the 11 import statements are completely unnecessary (and some of them might cause compiler errors, since you can't import methods, only classes).
 
Ma New
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was one I found online. Netbeans wont even let me compile it and run it. I have never worked with the JAI API. It is supposed to be easy I am just a little confused as where to start. I appreciate any help...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are errors it would be useful to know what those are. My previous post hints at the cause of some of them.

Has your boss given you a reason why this should be done in a JSP? Doing I/O (or Java computations in general) is considered bad design. JSPs really are not appropriate for that sort of thing.
 
Ma New
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I may have found a solution but to answer your question for doing the IO from the JSP was to make it as easy as possible to understand for our 3 interns. Our server also runs off the cache so isnt the JSP compiled into a servlet anyway.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the problem?

Servlets vs. JSP isn't about performance; it's about code maintainability and good design (MVC).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!