Hi guys.. As I told you in my previous image resize post, here is the latest program for creating the thumbnails of different sized with out any image distortion and lack of quality. Keep that in mind that the image with less quality will not be achieved with the quality. Quality source gives the quality thumbnail.

I've written this based on the existing programs and methods. This has been tested on Java 1.4 and 1.5 versions.

Just include the below class into your disk so that you can use this component in web programming like Servlets, Struts, Springs, JSPs or desktop programs.

If you've any issues email me @ doubtsplz@gmail.com

AmazingImageResizer.java


import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;

/**
* Perfect image resizer which will give good quality
* @author SANTHOSH REDDY MANDADI
* @version 1.0
* @since 16th June 2009
*/

public class AmazingImageResizer
{
/**
* A static method to create the thumbnail of the mentioned sizes
* @author SANTHOSH REDDY MANDADI
* @param sourceFile represents the original file from where the thumbanil to be created
* @param destFile represents the output file path to store the thumbnail
* @param newWidth represents the width
* @param newHeight represents the height
* @since 16th June 2009
*/
public static void createThumbnail(String sourceFile, String destFile,int newWidth, int newHeight) throws Exception
{
BufferedImage image=ImageIO.read(new File(sourceFile));
int width = image.getWidth();
int height = image.getHeight();

boolean isTranslucent = image.getType() != Transparency.OPAQUE;

if (newWidth >= width || newHeight >= height)
{
throw new IllegalArgumentException("newWidth and newHeight cannot be greater than the image dimensions");
}
else if (newWidth <= 0 || newHeight <= 0)
{
throw new IllegalArgumentException("newWidth and newHeight must be greater than 0");
}

BufferedImage thumb = image;
BufferedImage temp = null;
Graphics2D g2 = null;
Map map = new HashMap();
map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
map.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
map.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try
{
int previousWidth = width;
int previousHeight = height;
do
{
if (width > newWidth)
{
width /= 2;
if (width < newWidth)
{
width = newWidth;
}
}
if (height > newHeight)
{
height /= 2;
if (height < newHeight)
{
height = newHeight;
}
}
if (temp == null || isTranslucent)
{
if (g2 != null)
{
//do not need to wrap with finally
//outer finally block will ensure
//that resources are properly reclaimed
g2.dispose();
}
temp = createCompatibleImage(image, width, height);
g2 = temp.createGraphics();
g2.setRenderingHints(map);
//g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
}
g2.drawImage(thumb, 0, 0, width, height,0, 0, previousWidth, previousHeight, null);
previousWidth = width;
previousHeight = height;
thumb = temp;
} while (width != newWidth || height != newHeight);
} finally
{
g2.dispose();
}
if (width != thumb.getWidth() || height != thumb.getHeight())
{
temp = createCompatibleImage(image, width, height);
g2 = temp.createGraphics();
try
{
g2.setRenderingHints(map);
g2.drawImage(thumb, 0, 0, width, height, 0, 0, width, height, null);
}
finally
{
g2.dispose();
}
thumb = temp;
}
ImageIO.write(thumb, destFile.substring(destFile.lastIndexOf('.')+1), new FileOutputStream(destFile));
}
public static BufferedImage createCompatibleImage(BufferedImage image,int width, int height)
{
return isHeadless()?new BufferedImage(width, height, image.getType()):getGraphicsConfiguration().createCompatibleImage(width, height);
}
// Returns the graphics configuration for the primary screen
private static GraphicsConfiguration getGraphicsConfiguration()
{
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
private static boolean isHeadless()
{
return GraphicsEnvironment.isHeadless();
}
public static void main(String[] args) throws Exception
{
//Fetch the image height and width (I'm hardcoding them here, if you don't know how to get look at creatThumbnail method)
BufferedImage image=ImageIO.read(new File("Water.jpg"));
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
//Original size of the image is
int newWidth=200;
//Calculating the percentage of image width going to be reduced
double imgHeightPercentage= (newWidth/(double)imgWidth)*100;
//Calculating the height based on the above percentage
double imgTotalHeight=imgHeight*(imgHeightPercentage/100);
int newHeight=(int)Math.round(imgTotalHeight);
AmazingImageResizer.createThumbnail("Water.jpg", "aWater.jpg", newWidth, newHeight);
}
}

If you're not satisfied with the quality, try with my old logic, which you can find @ Perfect image resizer

I got very good quality either one of these logics. But current logic is always best to choose.

Click here to check out other programs that I've developed.