图像像素数据读取和打印

时间:2013-05-30 00:40:20

标签: java csv pixel filewriter printwriter

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

1 个答案:

答案 0 :(得分:2)

仔细看看marchThroughImage()中的for循环:) 你开始时我等于h,并且当我是&gt; = h时要求它停止。因此,实际上不会发生迭代。与j相同。

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