2012-07-11 62 views
1

我需要找到一種方法來計算圖像的長度和寬度(這是一個.jpg)我有文件的長度,但它沒有用,因爲我想創建一個數組來存儲有關每個我正在使用的圖像文件的像素。如何在java中獲得jpg的像素尺寸?

public void getImageData(){ 
    File f= new File("myPicture.jpg"); 
    this.length = (int) f.length(); 
     System.out.println("length of file = " + length); 
     BufferedImage image = null; 
     try { 
      image = ImageIO.read(f); 
      } 
     catch (IOException e) { 

      e.printStackTrace(); 
      } 

非常感謝!

+0

可能的重複http://stackoverflow.com/questions/1559253/java-imageio-getting-image-dimension-without-reading-the-entire-file – 2012-07-11 20:51:46

回答

3

一旦你得到了BufferedImage,你可以調用屬性getWidth()getHeight()它:

int imageWidth = image.getWidth(); 
int imageHeight = image.getHeight(); 

希望這有助於。

+0

謝謝你的工作!簡單的解決方案! – Magpie 2012-07-11 21:13:14

+1

如果答案有效,請檢查答案旁邊的綠色複選標記,以便人們知道您已找到解決方案! – Wires77 2012-07-11 21:14:48

+0

我沒有看到,但現在做了,謝謝。 – Magpie 2012-07-11 21:26:26