Java - 程序没有写下所有数据(最后删除 15 个位置) - 为什么?
Java - program does not write down all data (cuts 15 positions at the end) - why?
我是一个完全的新手,所以也许问题很明显:问题是我正在制作程序(JAVA,处理),它给我每个中的 R、G、B 的数量图片上选定的像素(每十行中的每个像素)。当我通过控制台打印输出时,它可以完美地处理所有数据,直到最后一行的 250 像素出现(图片宽度为 251),但是当我将其写入文件时,它会将数据剪切为 235 像素 - 剩下的 15 像素不是写下来。是缓冲存储器的问题吗?我怎样才能写入所有数据?
这是代码:
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
}
你试过在 updatePixels();
之前写 output.flush();
吗?
好的,感谢楼上的帮助。现在我必须更深入地研究这个,我希望程序也给我一个 CSV 文件,其中包含 table 的这种聚合:第一列将是测试图片上的 y 位置,第一行是 x 位置(逐个像素),下一行是 R 值,然后是 G,然后是 B。问题是我已经将它放入循环中以使其自动完成但我再次堆叠 - ArrayIndexOutOfBoundsException 我不该有的东西。这是代码:
PrintWriter 输出;
Table Tabela;
String i;
void setup()
{
output = createWriter("positions.txt"); //writedown tool enter
Tabela=new Table();
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
Tabela.addColumn("Y");
String i=Integer.toString(j);
Tabela.addColumn(i);
TableRow newRow=Tabela.addRow();
newRow.setInt("Y", Tabela.lastRowIndex());
newRow.setFloat(s,r);
newRow.setFloat(s,g);
newRow.setFloat(s,b);
output.println(s + " , "+ j +" , "+ 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(s + " , "+ j +" ; "+ r + " , " + g + " , " + b); // print the read data
}
}
output.flush();
saveTable(Tabela,"data/tabela.csv");
updatePixels(); // set changes
}
void keyPressed()
{
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
我是一个完全的新手,所以也许问题很明显:问题是我正在制作程序(JAVA,处理),它给我每个中的 R、G、B 的数量图片上选定的像素(每十行中的每个像素)。当我通过控制台打印输出时,它可以完美地处理所有数据,直到最后一行的 250 像素出现(图片宽度为 251),但是当我将其写入文件时,它会将数据剪切为 235 像素 - 剩下的 15 像素不是写下来。是缓冲存储器的问题吗?我怎样才能写入所有数据? 这是代码:
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
}
你试过在 updatePixels();
之前写 output.flush();
吗?
好的,感谢楼上的帮助。现在我必须更深入地研究这个,我希望程序也给我一个 CSV 文件,其中包含 table 的这种聚合:第一列将是测试图片上的 y 位置,第一行是 x 位置(逐个像素),下一行是 R 值,然后是 G,然后是 B。问题是我已经将它放入循环中以使其自动完成但我再次堆叠 - ArrayIndexOutOfBoundsException 我不该有的东西。这是代码: PrintWriter 输出;
Table Tabela;
String i;
void setup()
{
output = createWriter("positions.txt"); //writedown tool enter
Tabela=new Table();
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
Tabela.addColumn("Y");
String i=Integer.toString(j);
Tabela.addColumn(i);
TableRow newRow=Tabela.addRow();
newRow.setInt("Y", Tabela.lastRowIndex());
newRow.setFloat(s,r);
newRow.setFloat(s,g);
newRow.setFloat(s,b);
output.println(s + " , "+ j +" , "+ 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(s + " , "+ j +" ; "+ r + " , " + g + " , " + b); // print the read data
}
}
output.flush();
saveTable(Tabela,"data/tabela.csv");
updatePixels(); // set changes
}
void keyPressed()
{
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}