2012-07-11 25 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