Android中的PPM图片

时间:2011-07-17 20:57:45

标签: android image 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中正常打开...

2 个答案:

答案 0 :(得分:2)

我的代码相当愚蠢,因为我没有研究Java实际上是什么字符。 Java中的char不是一个简单的字节。当代码被修改为逐字节消耗时,它就可以工作。

答案 1 :(得分:1)

对于仍在努力解决这个问题的人来说,这是一个功能强大的解决方案,我设法将其与我在网上找到的其他代码合并:

public static Bitmap ReadBitmapFromPPM2(String file) throws IOException {
    //FileInputStream fs = new FileInputStream(file);
    BufferedInputStream reader = new BufferedInputStream(new FileInputStream(new File(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();

    int width = Integer.valueOf(widths);
    int height = Integer.valueOf(heights);
    int[] colors = new int[width * height];

    byte [] pixel = new byte[3];
    int len = 0;
    int cnt = 0;
    int total = 0;
    int[] rgb = new int[3];
    while ((len = reader.read(pixel)) > 0) {
        for (int i = 0; i < len; i ++) {
            rgb[cnt] = pixel[i]>=0?pixel[i]:(pixel[i] + 255);
            if ((++cnt) == 3) {
                cnt = 0;
                colors[total++] = Color.rgb(rgb[0], rgb[1], rgb[2]);
            }
        }
    }

    Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
    return bmp;
}