呜呼。我有这个很棒的代码,它将图像的所有像素的数据输出到 csv/txt 文件中。现在它没有在文件中显示任何内容。该文件完全空白。我想念什么?
import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Scanner;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
public class JavaWalkBufferedImageTest1 extends Component {
public static void main(String[] foo) {
File outFile = new File ("finall.txt");
try{
FileWriter fWriter = new FileWriter (outFile, true);
PrintWriter pWriter = new PrintWriter (fWriter);
new JavaWalkBufferedImageTest1(pWriter);
pWriter.close();
} catch(Exception e){ }
}
public void printPixelARGB(int pixel, PrintWriter pW4) {
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
pW4.print(red + ", " + green + ", " + blue);
}
private void marchThroughImage(BufferedImage image, PrintWriter pW3) {
int w = image.getWidth();
int h = image.getHeight();
for (int i = h; i < h; i++) {
for (int j = w; j < w; j++) {
pW3.print( j + ", " + i + ", "); //X,Y
int pixel = image.getRGB(j, i);
printPixelARGB(pixel, pW3); //red,green,blue
pW3.println("");
}
}
}
public JavaWalkBufferedImageTest1(PrintWriter pW2) {
try {
BufferedImage image = ImageIO.read(this.getClass().getResource("color.jpg"));
marchThroughImage(image, pW2);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}