Hi I have data of an image of size 640 * 48开发者_如何学Go0 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);
}
}
}
精彩评论