显示图像的RGB编号

时间:2015-04-01 00:32:57

标签: java image rgb pixels

我需要显示所选图片中每个像素的所有RGB数字,每个数字用空格分隔,并在图片的每一行之后使用println(break)。我已经想出并编写了代码以返回所有RGB数字,但我不知道如何在每行之后打破。这是我到目前为止的代码..

public void getColor()
{
  System.out.println("This picture's dimensions are: "+this.getWidth()+" by "+this.getHeight());
   for (int row=0; row < this.getHeight(); row++) // gives the number of rows
   {
     for (int col=0; col < this.getWidth(); col++)// gives the number of columns
     {
       Pixel pix = this.getPixel(col,row);        
       System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" ");
     } // end of inner loop
   }// end of outer loop
} // end of method

3 个答案:

答案 0 :(得分:2)

您需要将换行符放在最里面的for循环之外,因为您希望它在每行完成后运行。

public void getColor()
{
  System.out.println("This picture's dimensions are: "+this.getWidth()+" by "+this.getHeight());
   for (int row=0; row < this.getHeight(); row++) // gives the number of rows
   {

     for (int col=0; col < this.getWidth(); col++)// gives the number of columns
     {
       Pixel pix = this.getPixel(col,row);        
       System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" ");
     } // end of inner loop

    //After this ^^^ for loop runs, you've gone over the whole row.
    System.out.println();
   }// end of outer loop
} // end of method

答案 1 :(得分:0)

只需在内部循环和外部循环之间插入print语句,就可以在每行的末尾添加换行符。

public void getColor()
{
  System.out.println("This picture's dimensions are: "+this.getWidth()+" by     "+this.getHeight());
   for (int row=0; row < this.getHeight(); row++) // gives the number of rows
   {
     for (int col=0; col < this.getWidth(); col++)// gives the number of columns
     {
       Pixel pix = this.getPixel(col,row);        
       System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" ");
     } // end of inner loop
     System.out.print("\n"); //added line break to end of line.
   }// end of outer loop
} // end of method

答案 2 :(得分:0)

在你的col循环之后放这个:

是System.out.print(System.getProperty(&#34; line.separator&#34));

查看此页面: http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html