Hi Palani,

Could you explain what you mean by "distorted"?  I ran your testcase
and it seems to work as expected.

BTW, your code that calculates the scale factors and whatnot could be
greatly simiplified.  Since you're just doing simple scaling, there's
no need to use AffineTransform.  Instead, you can simply use the
"scaling" drawImage() variant.  (We should probably write up an article
that demonstrates the best approaches for image scaling...)

Here's the simplified code:
    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);

        // Calculate scale so image fits in a square area of
thumbNailSize
        int imageWidth = bufferedImage.getWidth();
        int imageHeight = bufferedImage.getHeight();

        int scaledWidth, scaledHeight;
        if (imageWidth > imageHeight) {
            scaledWidth = thumbNailSize;
            scaledHeight =
                (int)(thumbNailSize * (((double)imageHeight) /
imageWidth));
        } else {
            scaledWidth =
                (int)(thumbNailSize * (((double)imageWidth) /
imageHeight));
            scaledHeight = thumbNailSize;
        }

        BufferedImage scaledBufferedImage =
            new BufferedImage(scaledWidth, scaledHeight,
                              BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = scaledBufferedImage.createGraphics();
        g2d.drawImage(bufferedImage, 0, 0, scaledWidth, scaledHeight,
null);
        g2d.dispose();

        // Now write out scaled image to file
        ImageIO.write(scaledBufferedImage,"JPG",new File(destFileName));
    }

Thanks,
Chris

On Sep 19, 2004, at 11:51 PM, Palani Murugan wrote:
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".

=========================================================================== 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".

Reply via email to