Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets and thumbnails

 
fahim chowdhury
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've got some code at home to do this for jpeg and gif - I will post it tonite (GMT)
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic