본문 바로가기

Develop/Java(jsp,jmf)

JAI(Java Advanced Imaging) 로 썸네일 이미지 만들기 (100%)

먼저 jar 파일이 있어야 겟죠..?
다운받기--> http://www.oracle.com/technetwork/java/current-142188.html

공통 유틸에다가 다음 함수를 추가 해준다.

/*
*  썸네일 만들기 함수
*/
 public static boolean thumbConvert(String in, String out, int width, int height, String format){
     
  File saveFile = new File(out);

  RenderedOp rOp = JAI.create("fileload", in);
  BufferedImage im = rOp.getAsBufferedImage();


  float cvtWidth = 0.0f;
  float cvtHeight = 0.0f;


  if(im.getWidth() > im.getHeight()){
   cvtHeight = (float)height * ((float)im.getHeight() / (float)im.getWidth());
   cvtWidth = width;
  }
  else if(im.getWidth() < im.getHeight()){
   cvtWidth = (float)width * ((float)im.getWidth() / (float)im.getHeight());
   cvtHeight = height;
  }
  else{
   cvtWidth = width;
   cvtHeight = height;
  }


  BufferedImage thumb = new BufferedImage((int)cvtWidth, (int)cvtHeight, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = thumb.createGraphics();


  g2.drawImage(im, 0, 0, (int)cvtWidth, (int)cvtHeight, null);


  try{
   return ImageIO.write(thumb, format, saveFile);
  }
  catch(IOException io){
   System.out.println("예외 : " + io);
   return false;
  }
 }


이클립스에서는 ctrl+o 를 누르면 알아서 import 해주므로 생략...

그리고 사용할때는
 //썸네일 생성
     String loadFileName = 썸네일을 만들 원본 경로+파일명
     String thumbnailFileName = 썸네일을 만들 경로+파일명 지정     
     StringUtil.thumbConvert(loadFileName, thumbnailFileName, (int)가로크기, (int)세로크기, "jpg");

끝~~ by. xranma