Search code examples
javargbpixel

Java - program does not write down all data (cuts 15 positions at the end) - why?


I am a complete newbie so maybe the question is obvious: The thing is that I am making the program (JAVA, processing) that is to give me the amount of R, G, B in each selected pixel (each pixel in each ten row) on the picture. When I am printing the outputs over the console it works perfectly all data till the 250 pixel in last row are there (picture width is 251) but when I write it down to the file it cuts the data in 235 pixel - 15 remaining are not written down. Is it the question of buffer memory? How can I have all data written? Here is the code:

PrintWriter output;

void setup()

{
 output = createWriter("positions.txt");     //writedown tool enter
  size(251,201);                             // screen size setup
  PImage img = loadImage("sharp.jpg");       // implementing image Sketch->Add File
  image(img,0,0);                            // placeing the img on the screen
  loadPixels();                              // work on pixels

  for(int s=0; s < pixels.length; s=s+9*width)    // go through selected rows of pixels with intervals of 10 rows (0 to 0+9width)
  {
    for(int j=0; j < width; j++)               // go through each pixel in selected row
    {
    float r=red(pixels[s+j]);                  // read the red value from each chceked pixel
    float b=blue(pixels[s+j]);                 // read the blue value from each chceked pixel
    float g=green(pixels[j+s]);                // read the green value from each checked pixel

   output.println(j + " , "+ s +" , "+ r + " , " + g + " , " + b);  //write the data down to the file (selected data)

   color black = color(0, 0, 0);                              // checking, set a black color
   pixels[j+s]=black;                                         // checking, change the color of current pixel

  println(j + " , "+ s +" ; "+ r + " , " + g + " , " + b);    // print the read data

    }
    }
updatePixels();                                // set changes
}

void keyPressed()
{
output.flush();  // Writes the remaining data to the file
 output.close();  // Finishes the file
  exit();  // Stops the program
}

Solution

  • did you try writing output.flush(); just before updatePixels(); ?