H
hilz
Hi all,
After doing a search on how to create thumbnails from jpg's, i found the
code below.
If i run this code from a standalone java program, it works fine and it
creates the thumbnail,
but if i run it from inside a jsp page, it creates a black thumbnail with
nothing in it!!!!
does anyone know what could be the problem? it is driving me crazy.
thanks
hilz
//usage: java Thumbnail <file.jpg> <newfile.jpg> <MAXWIDTH>
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.iutputStream;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
class ThumbnailCreator {
public static void main(String[] args) {
createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
}
/**
* Reads an image in a file and creates
* a thumbnail in another file.
* @param orig The name of image file.
* @param thumb The name of thumbnail file.
* Will be created if necessary.
* @param maxDim The width and height of
* the thumbnail must
* be maxDim pixels or less.
*/
public static void createThumbnail( String orig, String thumb, int
maxDim) {
try {
// Get the image from a file.
java.awt.Image inImage = new
javax.swing.ImageIcon(orig).getImage();
// Determine the scale.
double scale = (double)maxDim/(double)inImage.getHeight(null);
if (inImage.getWidth(null) > inImage.getHeight(null)) {
scale = (double)maxDim/(double)inImage.getWidth(null);
}
// Determine size of new image.
//One of them
// should equal maxDim.
int scaledW = (int)(scale*inImage.getWidth(null));
int scaledH = (int)(scale*inImage.getHeight(null));
// Create an image buffer in
//which to paint on.
java.awt.image.BufferedImage outImage = new
java.awt.image.BufferedImage(scaledW,
scaledH,java.awt.image.BufferedImage.TYPE_INT_RGB);
// Set the scale.
java.awt.geom.AffineTransform tx = new
java.awt.geom.AffineTransform();
// If the image is smaller than
//the desired image size,
// don't bother scaling.
if (scale < 1.0d) {
tx.scale(scale, scale);
}
// Paint image.
java.awt.Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();
// JPEG-encode the image
//and write to file.
java.iutputStream os = new java.io.FileOutputStream(thumb);
com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(os);
encoder.encode(outImage);
os.close();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}
After doing a search on how to create thumbnails from jpg's, i found the
code below.
If i run this code from a standalone java program, it works fine and it
creates the thumbnail,
but if i run it from inside a jsp page, it creates a black thumbnail with
nothing in it!!!!
does anyone know what could be the problem? it is driving me crazy.
thanks
hilz
//usage: java Thumbnail <file.jpg> <newfile.jpg> <MAXWIDTH>
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.iutputStream;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
class ThumbnailCreator {
public static void main(String[] args) {
createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
}
/**
* Reads an image in a file and creates
* a thumbnail in another file.
* @param orig The name of image file.
* @param thumb The name of thumbnail file.
* Will be created if necessary.
* @param maxDim The width and height of
* the thumbnail must
* be maxDim pixels or less.
*/
public static void createThumbnail( String orig, String thumb, int
maxDim) {
try {
// Get the image from a file.
java.awt.Image inImage = new
javax.swing.ImageIcon(orig).getImage();
// Determine the scale.
double scale = (double)maxDim/(double)inImage.getHeight(null);
if (inImage.getWidth(null) > inImage.getHeight(null)) {
scale = (double)maxDim/(double)inImage.getWidth(null);
}
// Determine size of new image.
//One of them
// should equal maxDim.
int scaledW = (int)(scale*inImage.getWidth(null));
int scaledH = (int)(scale*inImage.getHeight(null));
// Create an image buffer in
//which to paint on.
java.awt.image.BufferedImage outImage = new
java.awt.image.BufferedImage(scaledW,
scaledH,java.awt.image.BufferedImage.TYPE_INT_RGB);
// Set the scale.
java.awt.geom.AffineTransform tx = new
java.awt.geom.AffineTransform();
// If the image is smaller than
//the desired image size,
// don't bother scaling.
if (scale < 1.0d) {
tx.scale(scale, scale);
}
// Paint image.
java.awt.Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();
// JPEG-encode the image
//and write to file.
java.iutputStream os = new java.io.FileOutputStream(thumb);
com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(os);
encoder.encode(outImage);
os.close();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}