The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Thumbnail problem

Posted by Ever green friend on June 28, 2001 at 11:43 AM


Hello friends
I have written a program which will Thumbnail an image.But i want
Thumbnail an complete directory.Here is the code.Thanks in advance
for help friends.Waiting a reply on my mail.


import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.widget.ScrollingImagePanel;
import java.lang.*;
import java.util.*;
import java.io.*;
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;

public class Thumb
{
public static void main(String[] args)
{
InputStream is = new FileInputStream("F:/Phot's/My Image Files/cmfam-img.gif");
SeekableStream s = SeekableStream.wrapInputStream(is, false);

// Create the ParameterBlock and add the SeekableStream to it.
ParameterBlock pb = new ParameterBlock();
pb.add(s);

// Perform the BMP operation
//op = JAI.create("BMP", pb);

createThumbnail( s , "F:/Phot's/My Image Files/newgiffile.jpg", 20);
// createThumbnail("F:/Phot's/My Image Files/bg1967_145.jpg", "F:/Phot's/My Image Files/new.jpg", 20);
// 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.
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));
int scaledH = (int)(scale * inImage.getHeight(null));

// 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);
}
}



Replies:
  • code kamal abuomar November 01, 2001 at 5:53 AM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us