Java 从灰度值构造图像

Java 从灰度值构造图像,java,image,image-processing,Java,Image,Image Processing,我已经从图像文件格式.3pi中提取了灰度值,并在下面的文件代码中编写了它们- public class ImageFile { public static void main(String[] args) throws IOException { FileInputStream fstream = new FileInputStream ("Z:\\20100204-000083-011.3pi"); DataInputStream in = new DataInputStrea

我已经从图像文件格式.3pi中提取了灰度值,并在下面的文件代码中编写了它们-

public class ImageFile {
public static void main(String[] args) throws IOException {
    FileInputStream fstream = new FileInputStream ("Z:\\20100204-000083-011.3pi");
    DataInputStream in = new DataInputStream (fstream);

    BufferedReader reader = new BufferedReader (new InputStreamReader (in));
    String str = "";
    String temp [];
    int counter = 0, NumberOfColumn = 0, NumberOfRow = 0;
    try {
      while (counter != 3) {
            str = reader.readLine();
            counter ++;
            if (counter == 2) {
                temp = str.split(" ");
                NumberOfRow = Integer.valueOf(temp[5].trim()).intValue();

            }
            else if (counter == 3) {
                temp = str.split(" ");
                NumberOfColumn = Integer.valueOf(temp[3].trim()).intValue();            
            }
        }
    } 

    catch (FileNotFoundException e){
        e.printStackTrace();
    }

    catch (IOException e) {
        e.printStackTrace();
    }

    //System.out.println ("Row : "+NumberOfRow);
    //System.out.println ("Column : "+NumberOfColumn);

    int found = 0, CurrentColumn = 0, CurrentRow = 0, GrayValue;
    int image [][] = new int [NumberOfRow][NumberOfColumn];

    FileInputStream fstream2 = new FileInputStream ("Z:\\20100204-000083-011.3pi");
    DataInputStream in2 = new DataInputStream (fstream2);
    BufferedReader reader2 = new BufferedReader (new InputStreamReader (in2));

    FileWriter fstream3 = new FileWriter("Z:\\Test.txt",true);
    BufferedWriter writer = new BufferedWriter (fstream3);
    while ((str = reader2.readLine()) != null) {            
        str = str.trim();
        temp = str.split(" ");
        if (temp [0].contentEquals("#:Profile:")) {
            CurrentColumn = Integer.valueOf(temp[1].trim()).intValue();
            //System.out.println ("Current Column : "+CurrentColumn);
            found = 1;
            continue;
        }
        else {
            if (found == 1) {
                CurrentRow = Integer.valueOf(temp[4].trim()).intValue();
                GrayValue = Integer.valueOf(temp[3].trim()).intValue();
                image [CurrentRow][CurrentColumn] = GrayValue;                  
            }
        }
    }
    for (int i= 0; i< NumberOfRow; i++){
        for (int j= 0; j< NumberOfColumn; j++){
            writer.write (image [i] [j]+" ");
        }
        writer.write("\n");
    }
    writer.close();
}
}

现在,我想用Test.txt文件中的灰度值信息创建一个jpg/bmp/任何其他图像。我怎样才能做到呢?谢谢你的帮助

Zereen应该提供许多关于Java中映像I/O的有用信息

如果您有想要使用的颜色的像素数据,您可以使用AWT的Graphics2D部分类来绘制AWT的BuffereImage部分,如前所述。然后可以使用ImageIO写入数据。因此:

try {
    BufferedImage off_Image = 
    new BufferedImage(100, 50, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = off_Image.createGraphics(); 
    for (int i= 0; i< NumberOfRow; i++) {
        for (int j= 0; j< NumberOfColumn; j++) {
            g2.setColor(new Color(....)); // here convert the value in image[i][j] to aRGB
            g2.draw(new Rectangle(i, j, 1, 1);
        }
    }
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    // handle exception
}

谢谢我最初处理的是一个3pi文件的数据,得到的是灰度值,而不是来自一个3pi文件的图像。所以,现在我在文本文件中有了灰度值,我需要从中生成一个图像。@Mah Zereen-我已经更新了我的答案,包含了关于使用Graphics2D绘制缓冲区图像的信息。这是一种方法。衷心感谢你。它工作得很好。非常感谢你。