2011. 2. 25. 17:53 PROGRAMMING
[JAVA] 이미지 리사이즈용 소스, 썸네일 만들기, THUMBNAIL, IMAGE, RESIZE

사용법은 패스~

import [패키지];

File fileSource=new File("C:\ORIGINAL\","SOURCE.JPG");
File fileThumb=new File("C:\TEST\","AHNDOORI.JPG");  
ImageUtil.resizeImage(fileSource,fileThumb,128,96);

 

package [패키지];

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;
 }

}

댓글을 달아 주세요

  1. 소스파일, 타겟파일, 넓이, 높이

    2011.04.28 15:23 [ ADDR : EDIT/ DEL : REPLY ]
  2. Les nouvelles au sujet des femmes solides chaussures de taille 12 est que: les magasins de chaussures en ligne aussi vendre des chaussures chics <a href="http://www.suprashoespascher.com" title="Supra Shoes Pas Cher">Supra Shoes Pas Cher</a>
    qui peut être porté dans des occasions différentes. Les motifs sont assez larges: de petits dessins bustier, lacé chaussures,<a href="http://www.suprashoespascher.com" title="supra shoes france">supra shoes france</a>

    2011.11.29 16:02 [ ADDR : EDIT/ DEL : REPLY ]

최근에 올라온 글

최근에 달린 댓글