2013-04-22 73 views
1

你好我想创造一个PROGRAMM读取的图像中的RGB值和后输出的Excel,像这样---图像>http://www.boydevlin.co.uk/images/screenshots/eascreen04.png填充一个ArrayList与图像的每个像素

为了实现这一我想我已经从图像中每个像素的RGB值读取到一个ArrayList 我想将它保存在以下顺序

例5x5px图片

01,02,03,04,05 
06,07,08,09,10 
11,12,13,14,15 
....... 

我媒体链接有这一点,但它不工作出正确可能有人helpe我与algorrithm

public class Engine { 

    private int x = 0; 
    private int y = 0; 
    private int count = 50; 
    private boolean isFinished = false; 
    ArrayList<Color> arr = new ArrayList<Color>(); 

    public void process(){ 
     BufferedImage img = null; 
     try { 
      img = ImageIO.read(new File("res/images.jpg")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      System.out.println("img file not found"); 
     } 

     while(isFinished = false){ 
     int rgb = img.getRGB(x, y); 
     Color c = new Color(rgb); 
     arr.add(c); 
     System.out.println("y:"+ y); 
     x++;} 
     if(x == 49){ 
      y++; 
      x = 0; 
      }else if(x == 49 && y == 49){ 
       isFinished = true; 
      } 
     } 

}; 
+0

你得到什么错误? – jmrodrigg 2013-04-22 09:53:01

回答

1

你需要知道,如果图像将成为大,ArrayList会非常大,更好地使用普通数组(你知道.. []),并使其成为两个微观的。 更好的是,如果您可以在适当的位置创建excel并且不将所有数据保存在数组中,只需在将数据写入控制台的地方设置适当的值即可。 我没有测试过代码,但应该没问题。 如果您收到任何异常内容,以便我们提供帮助。

尝试类似的东西:

public class Engine { 

    private int x = 0; 
    private int y = 0; 
    ArrayList<Color> arr = new ArrayList<Color>(); 

    public void process() { 
     BufferedImage img = null; 
     try { 
      img = ImageIO.read(new File("res/images.jpg")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      System.out.println("img file not found"); 
     } 

     for(int x=0;x<img.getWidth();x++){ 
      for(int y=0;y<img.getHeight();y++){ 
       int rgb = img.getRGB(x, y); 
       Color c = new Color(rgb); 
       arr.add(c); 
       System.out.println("x: "+ x + " y:" + y +" color: " + c); 
      } 
     } 
    } 
}; 
2

首先:你有一个错误在while

从它转换:

while (isFinished=false)

while (isFinished==false) 

第二:使用for循环,而不是while

for (int x = 0; x < img.getWidth(); x++) { 
      for (int y = 0; y < img.getHeight(); y++) { 
       int rgb = img.getRGB(x, y); 
       Color c = new Color(rgb); 
       arr.add(c); 
      } 

     } 

,如果你使用while循环想要的话,试试这个:

while (isFinished == false) { 
      int rgb = img.getRGB(x, y); 
      Color c = new Color(rgb); 
      arr.add(c); 
      x++; 
      if (x == img.getWidth()) { 
       y++; 
       x = 0; 
      } else if (x == img.getWidth() - 1 && y == img.getHeight() - 1) { 
       isFinished = true; 
      } 
     } 
+1

+1 for for循环。 – jmrodrigg 2013-04-22 10:02:21