Java 循环以读取RGB文本文件

Java 循环以读取RGB文本文件,java,image,rgb,pixel,Java,Image,Rgb,Pixel,我需要能够为每个像素选择一个填充RGB数字的.txt文件,然后使用RGB数字创建图像。我已经弄明白了一切,除了我不知道如何创建一个循环来实际读取和设置新图片的像素。这很令人沮丧,因为我知道这不应该这么难。这是我到目前为止的代码 public static Picture convertRGBPic() { // set up file for reading String fileName = FileChooser.pickAFile(); File file = new File

我需要能够为每个像素选择一个填充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
public静态图片转换器rgbpic()
{
//设置要读取的文件
字符串文件名=FileChooser.pickAFile();
文件=新文件(文件名);
//声明一个空图片文件
Picture pic=新图片();
试一试{
扫描仪pictRGB=新扫描仪(文件);
int width=pictRGB.nextInt();
int height=pictRGB.nextInt();
//嵌套循环读取RGB数字(无alpha)并设置下面图片的每个像素
对于(int row=0;row
从哪里拍摄
图片
像素
?用默认构造函数创建的
图片不太可能与文本文件的宽度和高度完全相同。这是我的问题。我不知道如何正确设置像素为什么不使用
java.awt.image.buffereImage
?这还不够。