4

ImageObserverを使用せずにJavaで(URLを介して)画像のheightandを取得しようとしています。width私の現在のコードは次のとおりです。

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()私が持っていないImageObserverが必要です。前もって感謝します。

4

3 に答える 3

10

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);

主な変更点は、メソッドImageIconとメソッドを使用することです。getIconWidthgetIconHeight

于 2010-07-26T16:02:34.910 に答える
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);
于 2010-07-26T16:03:44.843 に答える
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内のフォルダーです。

クレジットはJavaでの画像の高さと幅の取得に行きます

于 2013-08-13T08:30:49.920 に答える