Below is the code for Image Conversion for JPG format.........
This code works fine but it gives distorted Image.....
Is It possible to do Image Conversion without distortion ???
Note:
This Program takes 3 command line argument.......
***************************************************************************
java JPGImage "source file Name" "Destination file Name" "size of
Image"
***************************************************************************
***************************************************************************
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.geom.*;
public class JPGImage
{
public JPGImage(String srcFileName,String destFileName,int
thumbNailSize) throws Exception
{
// Read in original image to create thumbnail from
File inFile = new File(srcFileName);
BufferedImage bufferedImage = ImageIO.read(inFile);
bufferedImage.flush();
// Calculate scale so image fits in a square area of thumbNailSize
int imageWidth = bufferedImage.getWidth();
int imageHeight = bufferedImage.getHeight();
int componentWidth = thumbNailSize;
int componentHeight = thumbNailSize;
double scale = -1;
if ( imageWidth == componentWidth && imageHeight ==
componentHeight){
scale = 1;
}
else if ( imageWidth <= componentWidth && imageHeight <=
componentHeight)
{
double heightScale = ((double)componentWidth) / ((double)
imageWidth);
double widthScale = ((double)componentHeight) / ((double)
imageHeight);
if ( heightScale < widthScale ) scale = heightScale;
else scale = widthScale;
}
else if ( imageWidth > componentWidth && imageHeight <=
componentHeight)
{
double heightScale = ((double)componentWidth) / ((double)
imageWidth);
scale = heightScale;
}
else if ( imageWidth <= componentWidth && imageHeight >
componentHeight)
{
double widthScale = ((double)componentHeight) / ((double)
imageHeight);
scale = widthScale;
}
else
{
double heightScale = ((double)componentWidth) / ((double)
imageWidth);
int scaledHeight = (int)(((double)imageHeight) *
heightScale);
double widthScale = ((double)componentHeight) / ((double)
imageHeight);
int scaledWidth = (int)(((double)imageWidth) * widthScale);
if ( scaledWidth <= componentWidth ) scale = widthScale;
else scale = heightScale;
}
// Now create thumbnail
AffineTransform affineTransform = AffineTransform.getScaleInstance
(scale,scale);
AffineTransformOp affineTransformOp = new AffineTransformOp
(affineTransform,null);
BufferedImage scaledBufferedImage = affineTransformOp.filter
(bufferedImage,null);
int scaledWidth = scaledBufferedImage.getWidth();
int scaledHeight = scaledBufferedImage.getHeight();
int expectedWidth = (int)(imageWidth * scale);
int expectedHeight = (int)(imageHeight * scale);
if ( scaledWidth > expectedWidth || scaledHeight > expectedHeight )
scaledBufferedImage = scaledBufferedImage.getSubimage
(0,0,expectedWidth,expectedHeight);
// Now write out scaled image to file
ImageIO.write(scaledBufferedImage,"JPG",new File(destFileName));
}
public static void main(String[] args)
{
if ( args.length < 3 )
{
System.err.println("Usage: imageTest srcFile
DestFile size");
System.exit(-1);
}
try
{
int size = 0;
try
{
size = Integer.parseInt(args[2]);
}catch (NumberFormatException
numberFormatException) {}
JPGImage jpgImage = new JPGImage(args[0],args[1],size);
}catch (Exception exception)
{
System.out.println(exception);
exception.printStackTrace();
}
}
***************************************************************************
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".