thumnail creation

Hello Sir

I am using a java class to create the thumbnail image in my project.

Its creating but it doesn't show the image when i open it., only all black kind of image open up. My code is as shown below. Please give me some tips where i am wrong.

The code is as follows ::

import java.awt.Image;

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.io.OutputStream;

import java.io.FileOutputStream;

import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

class Thumbnail {

public static void main(String[] args) {

createThumbnail("C:\\Documents and Settings\\admin\\Desktop\\63708837RS870056789.jpeg", "C:\\Documents and Settings\\admin\\Desktop\\thumb.jpeg",400);

}

/**

* 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.

Image inImage = new 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));

System.out.println("w="+scaledW);

int scaledH = (int)(

scale*inImage.getHeight(null));

System.out.println("h="+scaledH);

// Create an image buffer in

//which to paint on.

BufferedImage outImage =

new BufferedImage(scaledW, scaledH,

BufferedImage.TYPE_INT_RGB);

// Set the scale.

AffineTransform tx =

new 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.

Graphics2D g2d =

outImage.createGraphics();

g2d.drawImage(inImage, tx, null);

g2d.dispose();

// JPEG-encode the image

//and write to file.

OutputStream os =

new FileOutputStream(thumb);

JPEGImageEncoder encoder =

JPEGCodec.createJPEGEncoder(os);

encoder.encode(outImage);

os.close();

} catch (IOException e) {

e.printStackTrace();

}

System.exit(0);

}

}

With Regards

[2965 byte] By [chahalkhushwindera] at [2007-11-27 6:08:19]
# 1
I can't help you with this specific problem, but http://www.google.com/search?q=thumbnail+java provides lot of code examples how to create thumbnails with Java. The first Google hit shows an nice standalone solution: http://schmidt.devlib.org/java/save-jpeg-thumbnail.html Take a look.
BalusCa at 2007-7-12 17:10:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
thanx a lot sir.:)
chahalkhushwindera at 2007-7-12 17:10:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...