从Grayvalues构造图像

时间:2011-02-07 19:36:29

标签: 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 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文件中使用Gray值信息创建jpg / bmp /任何其他图像。我怎样才能实现它?帮助赞赏。

Zereen

1 个答案:

答案 0 :(得分:0)

Java Image I/O API Guide应提供有关Java中图像I / O的大量有用信息。

如果您有像素数据(您想要使用的颜色),您可以使用类Graphics2D(AWT的一部分)来绘制BufferedImage(AWT的一部分),如上所述{ {3}}。然后,您可以使用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
}