2010-07-26 4 views
2

ImageObserver없이 Java에서 이미지 (URL을 통해) height 및 을 얻으려고합니다. 내 현재 코드는 다음과 같습니다ImageObserver없이 Java에서 이미지의 높이와 너비 받기

public static void main(String[] args) throws IOException { 
    // TODO Auto-generated method stub 

    File xmlImages = new File("C:\\images.xml"); 
    BufferedReader br = new BufferedReader(new FileReader(xmlImages)); 
    File output = new File("C:\\images.csv"); 
    BufferedWriter bw = new BufferedWriter(new FileWriter(output)); 
    StringBuffer sb = new StringBuffer(); 
    String line = null; 
    String newline = System.getProperty("line.separator"); 
    while((line = br.readLine()) != null){ 
     if(line.contains("http")){ 
      URL url = new URL(line.) 
      Image img = Toolkit.getDefaultToolkit().getImage(url); 
      sb.append(line + ","+ img.getHeight(null) + "," + img.getWidth(Null) + newline);    
     } 

    } 

    br.close(); 
    bw.write(sb.toString()); 
    bw.close(); 
} 

내가 내가 이미지가로드되고 난 height 및 이미지의 width을 볼 수있는 것을 볼 수 있어요 디버그 모드로 전환 할 때,하지만 난 반환 할 수없는 것 그들. getHeight()getWidth() 메쏘드에는 Image Observer가 필요합니다. 미리 감사드립니다.

답변

7

ImageIcon을 사용하면 이미지로드를 처리 할 수 ​​있습니다.

변경

Image img = Toolkit.getDefaultToolkit().getImage(url); 
sb.append(line + ","+ img.getHeight(null) + "," + img.getWidth(Null) + newline); 

ImageIcon img = new ImageIcon(url); 
sb.append(line + ","+ img.getIconHeight(null) + "," + img.getIconWidth(Null) + newline); 

메인 변화 ImageIcongetIconWidth, getIconHeight 방법을 사용하는 것이다.

+1

에게 있습니다. 나는 이것을 깨닫고 나 자신에게 질문에 답하기 위해 돌아왔다. 그래도 고마워. – Bilzac

2

Image image = Toolkit.getDefaultToolkit().getImage(image_url); 
    ImageIcon icon = new ImageIcon(image); 
    int height = icon.getIconHeight(); 
    int width = icon.getIconWidth(); 
    sb.append(line + ","+ height + "," + width + newline); 
0

하나는 폭 얻을 수있는 작업을해야 다음과 높이 URL을 검색에 어려움이있는 경우 코드를 사용하여 다음.

try { 

File f = new File(yourclassname.class.getResource("data/buildings.jpg").getPath()); 
BufferedImage image = ImageIO.read(f); 
int height = image.getHeight(); 
int width = image.getWidth(); 
System.out.println("Height : "+ height); 
System.out.println("Width : "+ width); 
       } 
catch (IOException io) { 
    io.printStackTrace(); 
    } 

참고 :데이터 이미지를 포함 /의 SRC의 폴더입니다.

신용은 Getting height and width of image in Java

관련 문제