File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Displaying a dynamic photo thumbnail in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Displaying a dynamic photo thumbnail in servlet" Watch "Displaying a dynamic photo thumbnail in servlet" New topic
Author

Displaying a dynamic photo thumbnail in servlet

DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I have a servlet that displays a picture in thumbnail form and then when the user clicks on it a pop up window shows the full sized pic. What I am doing for the thumbnail is forcing the pic, no matter what it's size, into a 125 x 125 pixel format but it makes some of the pics REALLY strange looking! I know with applets you can get the pic size but I was wondering if there was any way to do this with servlets & then scale the pic to fit into the thumbnail format so even though its been scaled down it still looks normal. Any suggestions would be appreciated!
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Hi DC,
First of all, you could use ImageIcon and load the GIF into your Servlet.
ImageIcon ii = new ImageIcon("/images/Picture.gif");
Then use the getIconHeight and getIconWidth methods this gives you x and y from there you can determine a scaled size that you feel is appropriate.
Personnally, I'd take each of the images and create a scaled version and store that. That way big Gifs don't have to be downloaded on the client to use for nothing more than a thumbnail. I believe Graphics Workshop is a good package to do the scaling with.


------------------
I Hope This Helps
Carl Trusiak, SCJP2


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I would do the thumbnails manually also but we have thousands of pics now & more being added by users every day. The problem is members create their ad & it is displayed immediately. To make thumbnails would be a full time job for someone! I found the imageIcon in the API & will try it out today..thanks much!
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
OK, I tried this thing out & got the strangest exception Ive ever seen. Here's the exception:
500 Servlet Exception
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:59)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:58)
at sun.awt.motif.MToolkit.<clinit>(MToolkit.java:57)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at java.awt.Toolkit$2.run(Toolkit.java:512)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:503)
at javax.swing.ImageIcon.<init>(ImageIcon.java:77)
at javax.swing.ImageIcon.<init>(ImageIcon.java:103)
at thumbTest.doPost(thumbTest.java:19)
at thumbTest.doGet(thumbTest.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:102)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:83)
at com.caucho.server.http.Invocation.service(Invocation.java:325)
at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:333)
at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:266)
at com.caucho.server.TcpConnection.run(TcpConnection.java:140)
at java.lang.Thread.run(Thread.java:484)
AND here's the code I used:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.swing.*;
public class thumbTest extends HttpServlet{

public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
String photoName = "testPho.jpg";
ImageIcon ii = new ImageIcon("/adPics/"+photoName);
int height = ii.getIconHeight();
int width = ii.getIconWidth();
double imagePercentage = 0.0;
if (height > width){
imagePercentage = 125 / height;
}
else{
imagePercentage = 125 / width;
}
out.println("<img border='0' src='/adPics/"+photoName+"' WIDTH='"+width * imagePercentage+"' height='"+height * imagePercentage+"'>");
out.println("<br>�<br>");
out.println("<img border='0' src='/adPics/"+photoName+"'>");
}
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
doPost(req, res);
}
};
Unfortunately I am not very good with swing so Im at a loss to see what is wrong here. It seems to be complaining about the intitialization of the percentage variable OR it's not finding the image & thats blowing it up because the variable is still 0.0. Should i use a ServletContext to get the path to the images??
[This message has been edited by DC Dalton (edited October 01, 2001).]
Aaron Anderson
Greenhorn

Joined: Feb 02, 2001
Posts: 8
Looks like you are running this on a UNIX box without x-windows configured. Unfortunately, you have to have X-windows configured to use the swing packages even if you are not displaying windows under JDK 1.3 on UNIX boxes. They fixed this problem with JDK 1.4

<B>Memo 13</B>
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Yessur this would be linux on apache! OK, back to the drawing board ..any suggestions here??
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
  1. A quick search of "x11 display variable" on google gave me this link, which answers your exact question: http://java.apache.org/faq/fom-serve/cache/73.html
    OK, I tried this thing out & got the strangest exception Ive ever seen.
  2. I noticed in your HTML code that it references a regular size image, and forces it into a thumbnail size when the user requests the page. Instead, why don't you use the ImageIcon strategy to automatically convert your users' images to thumbnail versions when they are uploaded. Then, when the user requests a page, send them the appropriate size, thumbnail or original.


  3. ------------------
    Miftah Khan
    - Sun Certified Programmer for the Java� 2 Platform
    - Sun Certified Web Component Developer for the Java� 2 Platform, Enterprise Edition
    [This message has been edited by Miftah Khan (edited October 01, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Displaying a dynamic photo thumbnail in servlet
 
Similar Threads
"Class names can serve as a valid identifier" Why?
images in database
Need a JAVA API for FLV Encoding
Code to create thumbnails
Applying affine transform to JPanel