循环读取RGB文本文件

时间:2015-04-12 18:32:53

标签: java image rgb pixel

我需要能够为每个像素选择一个填充RGB数字的.txt文件,然后使用RGB数字创建图像。我已经弄明白了,除了我不知道如何创建一个循环来实际读取和设置新图片的像素。这令人沮丧,因为我知道这不应该这么难。这是我到目前为止的代码......

public static Picture convertRGBPic()
{
  // set up file for reading
  String fileName = FileChooser.pickAFile();
  File file = new File(fileName);
  // declare an empty picture file
  Picture pic = new Picture();
  try {

    Scanner pictRGB = new Scanner(file);
    int width = pictRGB.nextInt();
    int height = pictRGB.nextInt();



// nested loops to read RGB numbers (no alpha) and set each pixel of the pic below

   for (int row = 0; row < pic.getHeight(); row++)
  {
    for (int col = 0; col < pic.getWidth(); col++)
    {
      Pixel fromPix = pic.getPixel(col , row );
      Pixel toPix = pic.getPixel(col,row); 
      toPix.setRed(fromPix.getRed());
      toPix.setGreen(fromPix.getGreen());
      toPix.setBlue(fromPix.getBlue());
    }
  }


   // end your nested loop

    pictRGB.close(); //close file after reading
  } // end of try and begin catch error if file cannot be read
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }//end catch

  return pic;
} // end of method

0 个答案:

没有答案