【问题标题】:converting pixel data to image in java在java中将像素数据转换为图像
【发布时间】:2011-08-16 04:28:50
【问题描述】:

嗨 我有一个大小为 640 * 480 像素的图像数据,数据格式为 0 和 1,在 txt 文件中。因此,一个文本文件中有 640*480=307200 个字符(0 和 1)。问题是 0 表示原始图像中没有任何内容(例如黑色背景),而 1 表示存在(例如用户站立并且它与用户 blob 相关),因此它不会被误认为 RGB 或字节数据。

我需要在 java 中读取并将其转换为大小为 640*480 像素的图像,其中 0 表示的像素可以设置为一种颜色(例如黑色),而 1 可以设置为另一种颜色(例如白色)。
我该怎么做???感谢您的帮助。

【问题讨论】:

  • 逐个字符读取txt文件并使用if-else语句相应地绘制图像。

标签: java image image-processing


【解决方案1】:

首先,你需要把它读进去。如果你知道它的宽度,你可以这样做:

BufferedReader in = new BufferedReader(new FileReader("myfile.txt"));
boolean[][] mask = new boolean[640][480];
int i = -1;
int count = 0;
while((i = in.read()) !- -1) {
    int x = count % 640;
    int y = count / 640;
    mask[x][y] = (i == '1');
    count++;
}

那你就可以这样画了

paint(Graphics g) {
    g.setColor(Color.BLACK);
    g.drawRect(0,0,640,480); // draw the black background

    // mask it with white
    g.setColor(Color.WHITE);
    for(int x = 0; x < 640); x++) {
        for(int y = 0; y < 480); y++) {
            if(mask[x][y]) g.drawRect(x,y,1,1);
        }
    }
}

【讨论】:

  • 谢谢你 Glowcoder,但我不知道如何使用paint(Graphics g) 功能..
猜你喜欢
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2016-03-29
  • 2017-04-24
相关资源
最近更新 更多