As part of my content management system, I need to be able to generate
thumbnails from existing JPEG files located on the filesystem.  A simple
chunk of code I found uses the java.awt package to handle this nicely,
but when I moved the compiled classes from my windowsXP testing machine
to a linux server, it no longer worked.  The code I have been using
employs the (apparently) deprecated Toolkit class.  

I have seen that things like JAI and ImageMagick can accomplish this
task, but it seems like overkill for something that is so simple, and
should be available as part of the core Java distribution. 

Does anyone employ simple code that accomplishes the functionality
implied by:

generateThumbnail(String inFile, String outFile, String sizePercent)

to create and save a jpeg to the filesystem at a different size?



The code that I have been using is:

makeThumbnail(String inFile, String thumbFile, String th, String
inQuality) throws Exception {

Image image = Toolkit.getDefaultToolkit().getImage(inFile);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);

int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
        
int thumbHeight = Integer.parseInt(th);
int thumbWidth = (int)(thumbHeight * imageRatio);

// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
        
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHi
nts.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(thumbFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
int quality = Integer.parseInt(inQuality);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);

}



it throws the following exception (only when executed on a linux server)
:


java.lang.NoClassDefFoundError
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:141)
        at java.awt.Toolkit$2.run(Toolkit.java:748)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:739)

Thanks for any advice...

Cameron


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to