1

呜呼。我有这个很棒的代码,它将图像的所有像素的数据输出到 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());
    }
  }
}
4

1 回答 1

2

仔细看看 MarchThroughImage() 中的 for 循环 :) 你从 i 等于 h 开始,并要求它在 i >= h 时停止。所以实际上没有迭代发生。与 j 相同。

从 0 开始,你应该得到一些输出。

于 2013-05-30T00:45:44.823 回答