2016년 12월 25일 일요일

[java]이미지 섬네일 생성하기 소스


나도 어디서 퍼왔는지 기억이 잘 나지 않는다. 포맷부분만 약간 수정 했다.
비율이 유지 되면서

thumbWidth 값과 thumbHeight 값은 알아서 셋팅 하면되고

 int thumbWidth;
 int thumbHeight;



final int RATIO = 0;
final int SAME = -1;




        /**
* src 파일을 읽어 dest 파일에 전달 받은 format 형식으로 썸네일을 생성한다.
         * 비율 유지
* 작성자 : 어셈블링블링
* @param src
* @param dest
* @param width
* @param height
* @throws IOException
*/
public void makeThumb(File src, File dest, String format) throws IOException {
        Image srcImg = null;
        String suffix =                                                                                                 src.getName().substring(src.getName().lastIndexOf('.')+1).toLowerCase();

        if (suffix.equals("bmp") || suffix.equals("png") || suffix.equals("gif")) {
            srcImg = ImageIO.read(src);
        } else {
            // BMP가 아닌 경우 ImageIcon을 활용해서 Image 생성
            // 이렇게 하는 이유는 getScaledInstance를 통해 구한 이미지를
            // PixelGrabber.grabPixels로 리사이즈 할때
            // 빠르게 처리하기 위함이다.
            srcImg = new ImageIcon(src.toURL()).getImage();
        }
       
        int srcWidth = srcImg.getWidth(null);
        int srcHeight = srcImg.getHeight(null);
       
        int destWidth = -1, destHeight = -1;
       
        if (thumbWidth == SAME) {
            destWidth = srcWidth;
        } else if (thumbWidth > 0) {
            destWidth = thumbWidth;
        }
       
        if (thumbHeight == SAME) {
            destHeight = srcHeight;
        } else if (thumbHeight > 0) {
            destHeight = thumbHeight;
        }
       
        if (thumbWidth == RATIO && thumbHeight == RATIO) {
            destWidth = srcWidth;
            destHeight = srcHeight;
        } else if (thumbWidth == RATIO) {
            double ratio = ((double)destHeight) / ((double)srcHeight);
            destWidth = (int)((double)srcWidth * ratio);
        } else if (thumbWidth == RATIO) {
            double ratio = ((double)destWidth) / ((double)srcWidth);
            destHeight = (int)((double)srcHeight * ratio);
        }
       
        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, format, dest);
       
    }


퍼갈때 댓글 하나씩 남겨주삼.

댓글 없음:

댓글 쓰기

[lunux]리눅스 폴더별 용량 확인

리눅스 폴더별 용량 확인 조회 하고자 하는 디렉토리 리스트가있는 경로로 이동후 du -h --max-depth=1