Android打开.pgm图片

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上显示了。



  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值