Neat Function I Wrote

Here's a neat function I wrote as part of the photo album part of the site. It takes an image, a width and height, and an output image and creates a thumbnail with size "width by height". I will add that no part of this was with aid :-) Except for the Java 1.4.2 API Specification and a little brain power and some memory about how to scale geometric objects...

public void writeThumbnail(ImageInputStream iis,int w, int h, ImageOutputStream ios) throws Exception {

BufferedImage img = ImageIO.read(iis);

BufferedImage tnail = null;

double sx,sy;
sx = ((double)w)/img.getWidth();
sy = ((double)h)/img.getHeight();

AffineTransform at = new AffineTransform();
at.setToScale(sx,sy);

AffineTransformOp xop = new AffineTransformOp(at,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

tnail = xop.createCompatibleDestImage(img,null);
tnail = xop.filter(img,tnail);

ImageIO.write(tnail,"JPEG",ios);

ios.close();
}


I should add that there is another function that takes two strings instead of Image streams, both full filenames. I could add one that takes just an input stream and output stream for both, not image input and output streams. That way you could potentially write a thumbnail over the internet :-P A practical example (I like practical) would be to enter a URL of an image on the internet and save a thumbnail of it on your computer.

[Update] Check out the much quicker load times on my album Trip to Lewes. If you've seen it before you'll know. The next step, per Doug's very angry suggestion, is to have some sort of easy image navigation system... We'll see if I want to do it.

[Final Update] I made the photo albums a little easier to navigate. I'm still stumped as to a good way to do it for this site. It goes along pretty well with how the rest of the site is laid out, so, I guess we'll hear from Doug about how I did :)

blog comments powered by Disqus