Android打开.pgm图片

来源:互联网 发布:dna数据库能找人吗 编辑:程序博客网 时间:2024/04/30 18:01

1.关于.pgm图片

.pgm图片格式:参见   点击打开链接

.pgm图片格式非常简单,它没有对图片进行压缩,因此一张.pgm图片会比其他显示内容相当的图片大很多。直接在java里面打开是很容易点击打开链接。但是我要在android手机上打开,让其显示在ImageView上,但是Android并没有提供打开.pgm图片的方法。原本的思路就是移植java代码,但是在Android中导入java.awt.*包是一件很麻烦的事情,基本不可行。因此,我只能通过图片的数据来构造这个图片。思路就是读取图片的像素数据,通过Android中Bitmap的构造方法,将其构造出来。


2.代码


首先:

借助一个类来解析.pgm格式的数据

public class PGM{char ch0,   ch1;int  width, height;int  maxpix;DataInputStream in;public void readPGMHeader(String name)    {    try  {    FileInputStream fin = new FileInputStream(name);            in  = new DataInputStream(fin);      ch0 = (char)in.readByte();      ch1 = (char)in.readByte();      if(ch0 != 'P' || ch1 != '5'){          System.out.print("Not a pgm image!"+" [0]="+ch0+", [1]="+ch1);          System.exit(0);      }      in.readByte();                  //读空格      char c = (char)in.readByte();            if(c == '#')                    //读注释行      {      do{      c = (char)in.readByte();      }while((c != '\n') && (c != '\r'));      c = (char)in.readByte();      }            //读出宽度      if(c < '0' || c > '9'){      System.out.print("Errow!");          System.exit(1);      }            int k = 0;      do{      k = k*10+c-'0';      c = (char)in.readByte();      }while(c >= '0' && c <= '9');      width = k;            //读出高度      c = (char)in.readByte();      if(c < '0' || c > '9'){      System.out.print("Errow!");          System.exit(1);      }                  k = 0;      do{      k = k*10+c-'0';      c = (char)in.readByte();      }while(c >= '0' && c <= '9');      height = k;            //读出灰度最大值(尚未使用)      c = (char)in.readByte();      if(c < '0' || c > '9'){      System.out.print("Errow!");          System.exit(1);      }                  k = 0;      do{      k = k*10 + c - '0';      c = (char)in.readByte();      }while(c >= '0' && c <= '9');      maxpix = k;      }  catch(IOException e1){System.out.println("Exception!");}        }        public void readPPMHeader(String name)    {    try  {    FileInputStream fin = new FileInputStream(name);            in  = new DataInputStream(fin);      ch0 = (char)in.readByte();      ch1 = (char)in.readByte();      if(ch0 != 'P' || ch1 != '6'){          System.out.print("Not a pgm image!"+" [0]="+ch0+", [1]="+ch1);          System.exit(0);      }      in.readByte();                  //读空格            //读出宽度      char c = (char)in.readByte();      if(c < '0' || c > '9'){      System.out.print("Errow!");          System.exit(1);      }            int k = 0;      do{      k = k*10+c-'0';      c = (char)in.readByte();      }while(c >= '0' && c <= '9');      width = k;            //读出高度      c = (char)in.readByte();      if(c < '0' || c > '9'){      System.out.print("Errow!");          System.exit(1);      }                  k = 0;      do{      k = k*10+c-'0';      c = (char)in.readByte();      }while(c >= '0' && c <= '9');      height = k;            //读出灰度最大值(尚未使用)      c = (char)in.readByte();      if(c < '0' || c > '9'){      System.out.print("Errow!");          System.exit(1);      }                  k = 0;      do{      k = k*10 + c - '0';      c = (char)in.readByte();      }while(c >= '0' && c <= '9');      maxpix = k;      }  catch(IOException e1){System.out.println("Exception!");}        }        /***************************************************************     * 读入.pgm或.ppm文件     * type 5:pgm, 6:ppm     ***************************************************************/    public int[] readData(int iw, int ih, int type)    {    int[] pixels = new int[iw*ih];    try{    if(type == 5)    {      //读入图像灰度数据, 并生成图像序列  for(int i = 0; i < iw*ih; i++)  {                    int b = in.readByte();               if(b < 0) b = b + 256;               pixels[i] = (255<<24)|(b<<16)|(b<<8)|b;            }    }  else if(type == 6)  {      for(int i = 0; i < iw*ih; i++)  {                    int r = in.readByte();               if(r < 0) r = r + 256;               int g = in.readByte();               if(g < 0) g = g + 256;               int b = in.readByte();               if(b < 0) b = b + 256;               pixels[i] = (255<<24)|(r<<16)|(g<<8)|b;            }        }         }catch(IOException e1){System.out.println("Exception!");}   return pixels;     }         public char getCh0()    {    return ch0;    }         public char getCh1()    {    return ch1;    }              public int getWidth()    {    return width;    }            public int getHeight()    {    return height;    }        public int getMaxpix()    {    return maxpix;    }}
从代码可以看出,这个类不但可以解析.pgm图片,还可以解析.ppm图片。

然后:
在android中构建Bitmap

        String path = "你的.pgm路径";        int iw, ih;        int[] pix;        PGM pgm = new PGM();        pgm.readPGMHeader(path);        iw = pgm.getWidth();        ih = pgm.getHeight();        pix = pgm.readData(iw, ih, 5);   //P5-Gray image        Bitmap bitmap = Bitmap.createBitmap(iw,ih, Bitmap.Config.ARGB_4444);        bitmap.setPixels(pix,0,iw,0,0,iw,ih);

到此:就可以在ImageView上显示了。



0 0
原创粉丝点击