본문 바로가기
프로그램/Java

이미지 품질을 유지하면서 썸네일(Thumbnail) 이미지 만들기

by 로드러너 2014. 5. 26.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

원본이 가지고 있는 퀄리티를 유지하면서 썸네일 이미지를 만드는 소스는 다음과 같습니다.



전달되는 인자는 원본이미지명(orgName), 썸네일이미지명(newName), 썸네일 이미지 폭(width)


//원본이미지 파일을 읽어들인다.

Image srcImg = ImageIO.read(new File(orgName));


//이미지 사이즈 계산

int imx = srcImg.getWidth(null);

int imy = srcImg.getHeight(null);

int height = width * imy / imx;

               

Image imgTarget = srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH); 

int pixels[] = new int[width * height]; 

PixelGrabber pg = new PixelGrabber(imgTarget, 0, 0, width, height, pixels, 0, width); 

try {

pg.grabPixels();

} catch (InterruptedException e) {

throw new IOException(e.getMessage());

        

BufferedImage thumImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

thumImg.setRGB(0, 0, width, height, pixels, 0, width); 


//썸네일 이미지 파일을 작성한다.

File thumtFile = new File(newName);

ImageIO.write(thumImg, "jpg", thumFile);



관련글

JAI를 이용해서 썸네일 이미지 만들기