티스토리 뷰

Back/Java

[JAVA] 이미지 리사이징

RAHM 2017. 8. 3. 15:46

String imgSourcePath= "test_image.jpg";       // 원본 이미지 파일명 (경로 포함)

String imgTargetPath= "test_imageNew.jpg";    // 이미지 파일명(경로 포함)

String imgFormat = "jpg";                     // 이미지 포맷. jpg, gif

int newWidth = 640;              // 이미지 넓이

int newHeight = 360;             // 이미지 높이




imgResize(imgSourcePath, newImgFilePath, imgFormat, newWidth, newHeight);



/**

 * Image Resize

 */

public void imgResize(String imgSourcePath, String imgTargetPath, String imgFormat, int newWidth, int newHeight)

{

    try

    {

        // 원본 이미지 가져오기

        Image imgSrc = ImageIO.read(new File(imgSourcePath));


        // 이미지 리사이즈

        // Image.SCALE_DEFAULT : 기본 이미지 스케일링 알고리즘 사용

        // Image.SCALE_FAST    : 이미지 부드러움보다 속도 우선

        // Image.SCALE_SMOOTH  : 속도보다 이미지 부드러움을 우선

        // Image.SCALE_AREA_AVERAGING  : 평균 알고리즘 사용

        Image resizeImage = imgSrc.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);


        // 이미지  저장하기

        BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

        Graphics g = newImage.getGraphics();

        g.drawImage(resizeImage, 0, 0, null);

        g.dispose();

        ImageIO.write(newImage, imgFormat, new File(imgTargetPath));

    }

    catch (Exception e)

    {

    }

}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함