问题描述
Hi
我有一个大小为640 * 480像素的图像数据,数据的格式为0s和1s,在txt文件中。因此,文本文件中有640 * 480 = 307200个字符(0和1)。问题是0表示原始图像中没有任何内容(比如黑色背景),1表示存在(例如用户站立且与用户blob有关),因此不要误用RGB或字节数据。
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.
我需要读取并将其转换为java中大小为640 * 480像素的图像,其中0表示的像素可以设置为一种颜色(比如黑色) )和1到其他(说白色)。
我该怎么办?谢谢你的帮助。
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++;
}
然后你可以这样画画
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);
}
}
}
这篇关于在java中将像素数据转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!