반응형

JAVA ImageIO 객체 활용

ImageIO.write() 메서드 중 파일로 이미지를 생성하는 예제이다.

static boolean write(RenderedImage im, String formatName, File output)
String imagePath = "이미지경로..";
BufferedImage image = null;

//이미지를 읽어와서 BufferedImage에 넣는다.
'URL imgURL = getClass().getResource(imagePath);
'image = ImageIO.read(imgURL);
int width = image.getWidth();
int height = image.getHeight();

//파일명 자르기
String fileNm = imagePath.substring(imagePath.lastIndexOf("/") + 1);
try {
    // 저장할 이미지의 크기와 타입을 잡아줌.
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    bufferedImage.createGraphics().drawImage(image, 0, 0, this);

    // 해당경로에 이미지를 저장함.
    ImageIO.write(bufferedImage, "jpg", new File("C:/saveImage/" + fileNm));
} catch(Exception e) {
    e.printStackTrace();
}
반응형

+ Recent posts