상세 컨텐츠

본문 제목

[jsp] 흔한 이미지 썸네일 만들기 ImageIO 활용 (JAI와 ImageIO 비교)

헉!!/jsp, java

by 권태성 2012. 10. 18. 17:43

본문

JAI로 썸네일을 만들어봤었는데 화질 차이가 있다고해서 ImageIO를 사용하여 다시 만들어봤습니다.


if (fileExt.equals("jpg") || fileExt.equals("bmp") || fileExt.equals("png") || fileExt.equals("gif")) {

 File destFile = new File(출력할 이미지 경로 + 파일명);

 File orgFile = new File(원본 이미지 경로 + 파일명); 

 Image srcImg = ImageIO.read(orgFile);

 int width = 100; //줄일 가로길이

 int height = 100; //줄일 세로길이


 int srcWidth = srcImg.getWidth(null);

 int srcHeight = srcImg.getHeight(null);

 int destWidth = -1, destHeight = -1;

 if (width < 0) {

  destWidth = srcWidth;

 } else if (width > 0) {

  destWidth = width;

 }

 if (height < 0) {

  destHeight = srcHeight;

 } else if (height > 0) {

  destHeight = height;

 }

 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());

 } catch (Exception e) {

  e.printStackTrace();

 }

 BufferedImage destImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);

 destImg.setRGB(0, 0, destWidth, destHeight, pixels, 0, destWidth);

 ImageIO.write(destImg, "jpg", destFile);

}

소스는 이렇습니다.

원본 파일과 출력 할 경로를 File 객체로 만든다음 원본 파일을 ImageIO에서 read로 불러온다음 크기를 줄이는 작업을 합니다.

마지막에 ImageIO.write를 이용하여 출력 할 경로에 이미지를 저장하는건 JAI를 이용할때와 같네요.



아래는 JAI와 ImageIO로 리사이징한 이미지 입니다.

         

JAI              ImageIO

원본 이미지 용량 : 184KB (500x666)

JAI 이미지 용량 : 4.72KB (100x100)

ImageIO 이미지 용량 : 3.79KB (100x100)


좌측이 JAI를 이용하여 리사이징 한것이고 우측이 ImageIO를 이용하여 리사이징 한 것 입니다.

JAI 쪽은 선명하기는 하지만 작은 이미지이기 때문에 선명함이 부담스럽게 보입니다.

반면에 ImageIO쪽은 전체적으로 블러 처리가 되어 선명하지는 않지만 용량을 줄이고 이미지가 부드러워 졌습니다. 

썸네일 이미지를 사용하는 이유가 용량으로 인한 페이지 로딩속도를 줄이고자 사용하는 것이니

용도에 맞게 용량을 더 줄이고자 하면 ImageIO를 상관없다 하시는 분은 JAI도 문제 없을것 같네요.






관련글 더보기