Skip to main content

[JAVA2D] Is It possible to do Image Conversion without distortion...

1 reply [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chris Campbell

Hi Palani,

I assume you are referring to the artifacts introduced from the scaling
process. This will always be the case when you scale an image to be
larger than the original, since you cannot preserve the same
quality/resolution. I think what you are looking for is some sort of
smoothing/filtering, and this is exactly what you get if you enable
bilinear or bicubic filtering. That can be accomplished by setting a
RenderingHint right before you scale the image, like this:

Graphics2D g2d = scaledBufferedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(bufferedImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();

Hopefully that will improve the quality of your scaled images.

Thanks,
Chris

On Sep 21, 2004, at 8:45 PM, pal_magic@yahoo.com wrote:
> Hi Chris,
>  
> When I execute this program for a size of 500 , the Image looks
> distorted, it means the Image is not as it was clear in the original
> Image.
> I am attaching the sample
> Images(image1.jpg,image2.jpg,image3.jpg) with this mail.
>  
> image1.jpg - Orginal Image
> image2.jpg - After enlargment with a size of 500
> image3.jpg - After enlargment with a size of 300
>  
> Thanks,
> Palani Murugan.
>
>
> Chris Campbell wrote:
> 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 listserv@java.sun.com and include in
> the
> > body
> > of the message "signoff JAVA2D-INTEREST". For general help, send
> > email to
> > listserv@java.sun.com and include in the body of the message "help".
>
> =======================================================================
> ====
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body
> of the message "signoff JAVA2D-INTEREST". For general help, send email
> to
> listserv@java.sun.com and include in the body of the message "help".
>
> ALL-NEW Yahoo! Messenger - all new features - even more fun!
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".