사용법은 패스~
File fileSource=new File("C:\ORIGINAL\","SOURCE.JPG");
File fileThumb=new File("C:\TEST\","AHNDOORI.JPG");
ImageUtil.resizeImage(fileSource,fileThumb,128,96);
import java.io.File;
import java.io.IOException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageUtil{
public static final int RATIO=0;
public static final int SAME=-1;
// 소스파일, 타겟파일, 최대값
public static void resizeImage(File src,File dest,int boxsize) throws IOException{
int width=0;
int height=0;
Image srcImg=setImage(src);
int srcWidth=srcImg.getWidth(null);
int srcHeight=srcImg.getHeight(null);
if(srcWidth>srcHeight){
width=boxsize;
height=(int) ((double) boxsize/(double) srcWidth);
}else if(srcWidth<srcHeight){
width=(int) ((double) boxsize/(double) srcHeight);
height=boxsize;
}else {
width=boxsize;
height=boxsize;
}
try{
if(srcWidth<=boxsize && srcHeight<=boxsize) resizeImage(src, dest, -1, -1);
else resizeImage(src, dest, width, height);
}catch(IOException e){
throw e;
}
}
// 소스파일, 타겟파일, 넓이, 높이
public static void resizeImage(File src,File dest,int width,int height) throws IOException{
Image srcImg=setImage(src);
int srcWidth=srcImg.getWidth(null);
int srcHeight=srcImg.getHeight(null);
int destWidth=-1, destHeight=-1;
if(width==SAME) destWidth=srcWidth;
else if(width>0) destWidth=width;
if(height==SAME) destHeight=srcHeight;
else if(height>0) destHeight=height;
if(width==RATIO && height==RATIO){
destWidth=srcWidth;
destHeight=srcHeight;
}else if(width==RATIO){
double ratio=((double) destHeight)/((double) srcHeight);
destWidth=(int) ((double) srcWidth*ratio)-1;
}else if(height==RATIO){
double ratio=((double) destWidth)/((double) srcWidth);
destHeight=(int) ((double) srcHeight*ratio)-1;
}
Image imgTarget=srcImg.getScaledInstance(destWidth,destHeight,Image.SCALE_SMOOTH);
int pixels[]=new int[destWidth*destHeight];
PixelGrabber pg=new PixelGrabber(imgTarget, 0, 0, destWidth, destHeight, pixels, 0, destWidth);
try{
pg.grabPixels();
}catch(InterruptedException e){
throw new IOException(e.getMessage());
}
BufferedImage destImg=new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
destImg.setRGB(0, 0, destWidth, destHeight, pixels, 0, destWidth);
ImageIO.write(destImg, "jpg", dest);
}
private static Image setImage(File src) throws IOException{
Image srcImg=null;
String suffix=src.getName().substring(src.getName().lastIndexOf('.')+1).toLowerCase();
if(suffix.equals("bmp")) srcImg=ImageIO.read(src);
else srcImg=new ImageIcon(src.toURI().toURL()).getImage();
return srcImg;
}
}