IT/자바

[자바] 이미지 사이즈 알아보는 방법

Beautifulkim 2018. 6. 28. 23:25
1
2
3
4
5
6
7
8
9
10
 
 
 Image img = newImageIcon(이미지경로).getImage();
 System.out.println( img.getWidth(null));
 
 System.out.println( img.getHeigt(null));
 
 
 
출처: http://linguist79.tistory.com/1230 [파미유의 IT LIFE]
cs


1
2
3
4
5
6
7
8
9
10
11
try{
     File file = newFile(이미지경로);          
     BufferedImage bi = ImageIO.read( file );
     System.out.println( bi.getWidth() + “,” + bi.getHeight() );     
}
catch( Exception e ) {
     System.out.println(“이미지 파일이 아닙니다.”);
}
  
 
출처: http://linguist79.tistory.com/1230 [파미유의 IT LIFE]
cs


첫번째 방법은 Image 라이브러리로 바로 읽어오는 방법이며 두번째는 파일객체로서 버퍼이미지를 사용해서 읽어오는 방법입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
위에 말씀드린 방법은 로컬에서만 사용할 수 있으니 주의해주세요. 웹에서는 아래와 같은 방법으로 사용하시면 됩니다.
 
try 
{
    URL url = new URL("이미지주소");
    Image image = ImageIO.read(url);
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    System.out.println("width = " + width + ", height = " + height);
catch (Exception e) 
{
            System.out.println("파일이 없습니다.");
 
위와 같이 자바 이미지사이즈를 알아볼 수 있습니다. 참고하셔서 유용하게 사용하시기 바랍니다.
출처: http://linguist79.tistory.com/1230 [파미유의 IT LIFE]
cs