Hi

I have data of an image of size 640 * 480 pixels, the data is in the format of 0s and 1s, in a txt file. Therefore there are 640*480=307200 characters (0s and 1s) in a text file. The catch is that 0 means that there was nothing in the original image (say black background) and 1 means there is (say a user is standing and it is pertaining to the user blob) and hence its NOT to be mistaken with RGB or byte data.

I need to read and convert this into an image of size 640*480 pixels in java,where pixels indicated by 0 may be set to one color (say black) and 1 to other (say white).

How do I do it??? thanks for help.

解决方案

First, you need to read it in. If you know it's width, you can do something like this:

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++;

}

Then you can paint like this

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);

}

}

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐