【问题标题】:PPM Images in AndroidAndroid 中的 PPM 图像
【发布时间】:2011-10-07 06:58:10
【问题描述】:

我正在尝试在 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 中正确打开...

【问题讨论】:

  • 你可以得到netpbm toolkit的完整源码,无需猜测。
  • 我确实遇到过这个问题,但是当我在寻找一个相当简单的解决方案时,考虑到分布式核心代码,它出奇地复杂,但感谢您的指点。

标签: android image ppm


【解决方案1】:

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

【讨论】:

  • 嗨,我知道它太长了,但你能告诉我你的解决方案的代码 sn-p 吗?我也有同样的问题,想看看你解决问题的方法。
  • 我很久以前更改了该代码以直接处理 rgb 位图。我相信这只是用字节交换字符的问题,但我现在不记得了。您是从本地获取此图像吗?
【解决方案2】:

对于仍在为此苦苦挣扎的人,这是我设法将其与我在网上找到的其他一些代码合并创建的一个有效解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2014-01-15
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    相关资源
    最近更新 更多