Android 中的 PPM 图像
我正在尝试在 Android 中打开 .ppm 图像(便携式像素图)。我已经破译了足够的格式来创建这个:
public static Bitmap ReadBitmapFromPPM(String file) throws IOException
{
//FileInputStream fs = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new FileReader(file));
if (reader.read() != 'P' || reader.read() != '6')
return null;
reader.read(); //Eat newline
String widths = "", heights = "";
char temp;
while ((temp = (char)reader.read()) != ' ')
widths += temp;
while ((temp = (char)reader.read()) >= '0' && temp <= '9')
heights += temp;
if (reader.read() != '2' || reader.read() != '5' || reader.read() != '5')
return null;
reader.read(); //Eat the last newline
int width = Integer.parseInt(widths);
int height = Integer.parseInt(heights);
int[] colors = new int[width*height];
//Read in the pixels
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char[] pixel = new char[3];
reader.read(pixel);
/*
int red = reader.read();
int green = reader.read();
int blue = reader.read();
byte r = (byte)red;
byte g = (byte)green;
byte b = (byte)blue;*/
colors[y*width + x] = //(255 << 24) | //A
(pixel[0]&0x0ff << 16) | //R
(pixel[1]&0x0ff << 8) | //G
(pixel[2]&0x0ff); //B
}
}
Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
我已经到了解码像素的地步,但是即使是第一个像素的绿色和蓝色值的 ascii 值也是 16 位最大值(使用 .read 时为 65535) ())。正如你所看到的,我尝试了很多方法来深入了解合适的颜色值,但没有成功。
当我查看 ppm 中的值时,第二个和第三个字段中的字符很奇怪。有谁知道我在这里误入歧途吗? ppm 在 Photoshop 中正确打开...
I'm trying to open .ppm images (Portable pixmaps) in Android. I've deciphered the format enough to create this:
public static Bitmap ReadBitmapFromPPM(String file) throws IOException
{
//FileInputStream fs = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new FileReader(file));
if (reader.read() != 'P' || reader.read() != '6')
return null;
reader.read(); //Eat newline
String widths = "", heights = "";
char temp;
while ((temp = (char)reader.read()) != ' ')
widths += temp;
while ((temp = (char)reader.read()) >= '0' && temp <= '9')
heights += temp;
if (reader.read() != '2' || reader.read() != '5' || reader.read() != '5')
return null;
reader.read(); //Eat the last newline
int width = Integer.parseInt(widths);
int height = Integer.parseInt(heights);
int[] colors = new int[width*height];
//Read in the pixels
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char[] pixel = new char[3];
reader.read(pixel);
/*
int red = reader.read();
int green = reader.read();
int blue = reader.read();
byte r = (byte)red;
byte g = (byte)green;
byte b = (byte)blue;*/
colors[y*width + x] = //(255 << 24) | //A
(pixel[0]&0x0ff << 16) | //R
(pixel[1]&0x0ff << 8) | //G
(pixel[2]&0x0ff); //B
}
}
Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
I get to the point where I'm decoding the pixels, but the ascii values of the green and blue values for even the first pixel are 16 bit max value (65535 when using .read()). As you can see I've tried a lot of things to drill down to a decent value for the colors, but no luck.
When I look at the values in the ppm the characters in the second and third fields are strange. Does anyone know where I'm going astray here? The ppm opens properly in photoshop...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于仍在为此苦苦挣扎的人,这是一个功能解决方案,我设法将其与我在网上找到的其他一些代码合并:
for anyone still struggling with this, here is a functioning solution that I managed to create merging this with some other code I found online:
我的代码相当愚蠢,因为我没有研究 Java 中的 char 实际上是什么。 Java中的char并不是一个简单的字节。当代码被修改为逐字节消耗时,它就可以工作了。
My code was rather foolish since I didn't research what a char in Java actually was. char in Java is not a simple byte. When the code is modified to consume byte by byte it works.