Re: Dynamic Image resizing for thumbnail view with correct Aspect...
Re: Dynamic Image resizing for thumbnail view with correct Aspect...
- Subject: Re: Dynamic Image resizing for thumbnail view with correct Aspect...
- From: Florijan Stamenkovic <email@hidden>
- Date: Wed, 27 Jul 2005 11:23:44 +0200
On Jul 26, 2005, at 20:03, Mike Schrag wrote:
If you mean you just want the browser to show them smaller (i.e. users
will still be sent the 700 px wide version, but it will look smaller),
then you can just specify width = "200" on your WOImage and the browse
will adjust the height proportionally. If you want access to image
sizes and don't mind private frameworks, you can use
com.webobjects.appserver._private.WOImageInfo:
If however that is a too big bandwidth hit for you (ie you are listing
a bunch of images), you might want to shrink your images
programatically and send the small versions... Here is an excerpt of an
image control class that I use for myself for those things. The rest of
the class is project specific stuff that takes certain EOs and handles
images related to them.
Cheers
Flor
//
// ImageControl.java
// DvisBuild1
//
// Created by Florijan Stamenkovic on 2005 04 6.
// Copyright (c) 2005 CNG Havaso Ltd. All rights reserved.
//
import com.webobjects.foundation.*;
//import file handling classes
import java.io.File;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
//import pdf decoder, will require the jPedal libraries of some kind,
OpenSource as far as I can remember
import org.jpedal.PdfDecoder;
//import java awt classes
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;
//import java jpeg encoding and decoding classes
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGDecodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGCodec;
public abstract class ImageControl
{
protected static final float DEFAULT_JPEG_QUALITY = 0.75f;
protected static final int DEFAULT_THUMBNAIL_HEIGHT = 150;
/**A method that decodes the first page of PDF data into a
BufferedImage instance**/ public static BufferedImage
getImageForPdfData(NSData data) throws java.lang.Exception
{
PdfDecoder decoder = new PdfDecoder();
decoder.setExtractionMode( 0, 72, 1 );
decoder.openPdfArray( data.bytes() );
decoder.decodePage( 1 );
return decoder.getPageAsImage(1);
}
/**A method that scales a Buffered image and takes the required height
as a refference point**/
public static BufferedImage getScaledImageWithHeight(BufferedImage
image, int height) throws java.lang.Exception
{
int width = (int)(((float)image.getWidth() / (float)image.getHeight()
) * height);
Image scaledImage = image.getScaledInstance( width, height,
BufferedImage.SCALE_SMOOTH );
BufferedImage outImage = new BufferedImage( width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)outImage.createGraphics();
g2.drawImage( scaledImage, 0, 0, null );
return outImage;
}
/**JPEG encoder, encodes a BufferedImage class instance and returns
JPEG data**/
public static NSData encodeImageToJPEGDataWithQuality(BufferedImage
image, float quality) throws java.lang.Exception
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
try
{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam p = JPEGCodec.getDefaultJPEGEncodeParam(image);
p.setQuality(quality, false);
encoder.encode(image,p);
}
catch(Exception e)
{
System.err.println(new NSTimestamp()+"***FILE IO -
ImageControlCLASS***: Error while encoding data to JPEG. Throwing
exception.");
throw new java.lang.Exception("Error while encoding data to JPEG");
}
finally
{
os.close();
}
return new NSData(os.toByteArray() );
}
/**A method that decodes jpeg data. Possible exception throwing if
unable to decode**/
public static BufferedImage getImageForJPEGData(NSData data) throws
java.lang.Exception
{
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(data.stream());
return decoder.decodeAsBufferedImage();
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden