import com.mortennobel.imagescaling.AdvancedResizeOp;
import com.mortennobel.imagescaling.ResampleOp;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class Test {
public static void main(String[] args) throws IOException, Exception {
Test tt = new Test();
String sourceImagePath = "D:\\tt.jpg";
String destinationPath = "D:\\tt1";
String smallPath = "D:\\tt2";
int pos = sourceImagePath.lastIndexOf(".");
String imgFormat = sourceImagePath.substring(pos + 1);
String thumbnailImage = destinationPath.concat(".").concat(imgFormat);
String smallImg = smallPath.concat(".").concat(imgFormat);
tt.getThumbnail(sourceImagePath, thumbnailImage, imgFormat, 500, 400);
tt.getThumbnail(sourceImagePath, smallImg, imgFormat, 50, 50);
}
public void getThumbnail(String sourceImage, String thumbnailImage, String imgFormat, int picWidth, int picHeight)
throws Exception {
System.out.println(sourceImage + " " + thumbnailImage + " " + picWidth + " " + picHeight);
Image image = javax.imageio.ImageIO.read(new File(sourceImage));
double thumbRatio = (double) picWidth / (double) picHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
picHeight = (int) (picWidth / imageRatio);
} else {
picWidth = (int) (picHeight * imageRatio);
}
if ((imageWidth < picWidth) && (imageHeight < picHeight)) {
picWidth = imageWidth;
picHeight = imageHeight;
} else if (imageWidth < picWidth) {
picWidth = imageWidth;
} else if (imageHeight < picHeight) {
picHeight = imageHeight;
}
File f = new File(sourceImage);
BufferedImage src = ImageIO.read(f);
ResampleOp resampleOp = new ResampleOp(picWidth, picHeight);
resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Soft);
BufferedImage rescaled = resampleOp.filter(src, null);
ImageIO.write(rescaled, imgFormat, new File(thumbnailImage));
}
}