aspose file tools*
The moose likes Servlets and the fly likes servlets and thumbnails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and thumbnails" Watch "servlets and thumbnails" New topic
Author

servlets and thumbnails

fahim chowdhury
Greenhorn

Joined: Jul 14, 2002
Posts: 9
Let's say that I have large image files on my server. Does Java servlet technology have the capacity to send back, on the response object, just a thumbnail of a particular image? I thought about writing an HTML image tag that would determime smaller dimensions for the image, but that would actually send the entire image first before cropping it on the client side (so I am told). Any suggestions?
P.S. I just discovered your site and I think you guys are fabulous. Keep up the great work!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61217
    
  66

It is true that if you use the HTML IMG tag's height and width attibutes to create smaller versions of your image as a thumbnail that the entire image is downloaded to the client and then resized (not cropped) by the browser to the specified size for display.
Servlet and JSP technology would indeed give you the abililty to define your own custom "THUMBNAIL" tag and to write code that would resize the image on the server-side for shipping to the client.
Search this site and you will find quite a few threads on sending images as the response of a servlet request. As far as image manipulation on the server-side, perhaps the Java Advanced Imaging API could help you.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
i've got some code at home to do this for jpeg and gif - I will post it tonite (GMT)


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
fc21
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


Dave
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
I hope this gives you an idea of where to start. This is what I based my code on...(thanks richard huss)
package uk.co.talyllyn.gallery;
import com.sun.image.codec.jpeg.*;
import java.io.*;
public class JPEGSizer extends AbstractSizer {
private JPEGDecodeParam params;
public JPEGSizer(String filename) {
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder
(new FileInputStream(filename));
decoder.decodeAsRaster();
params = decoder.getJPEGDecodeParam();
width = params.getWidth();
height = params.getHeight();
} catch (IOException e) {
width = 0;
height = 0;
}
}
public static void main(String args[]) throws Exception {
JPEGSizer sizer = new JPEGSizer(args[0]);
System.out.println("Width " + sizer.getWidth());
System.out.println("Height " + sizer.getHeight());
}
}
package uk.co.talyllyn.gallery;
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
public class PhotoResizer {
private String oldFile, newFile;

public PhotoResizer(String oldFile, String newFile) {
this.oldFile = oldFile;
this.newFile = newFile;
}

public void writeScaledImage() {
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder
(new FileInputStream(oldFile));
BufferedImage originalImage = decoder.decodeAsBufferedImage();
JPEGDecodeParam params = decoder.getJPEGDecodeParam();
int width = params.getWidth();
int height = params.getHeight();
double scale = 100.0 / width;
ColorModel cm = originalImage.getColorModel();
AffineTransform transform = AffineTransform.getScaleInstance(scale,scale);
AffineTransformOp scaler =
new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage scaledImage =
scaler.createCompatibleDestImage(originalImage, cm);
scaler.filter(originalImage, scaledImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder
(new FileOutputStream(newFile));
encoder.encode(scaledImage);
} catch (IOException e) {
System.out.println("Oops");
}
}

public static void main(String args[]) {
new PhotoResizer("\\Documents and Settings\\richard\\My Documents\\TR Web Site\\images\\jpeg\\" + args[0] + ".jpg",
args[0] + "_sml.jpg").writeScaledImage();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlets and thumbnails