繁体   English   中英

如何将TIFF图像文件转换为Android位图

[英]How to converting TIFF image file to Bitmap Android

我想将SDcard上的图像转换为位图。 我正在使用下面的代码

String filepath = "/storage/emulated/0/Download/sample2.tif";
    Bitmap bm = BitmapFactory.decodeFile(filepath);

对于扩展名为.jpg或.png的文件,此代码正常工作,但对于.tif或.tiff的文件则无效。 对于TIFF文件, bm变为null,无例外,所有文件路径均无效。

Android BitmapFactory在解码TIFF图像方面是否有任何限制?

也试过了decodeByteArray and decodeStream ,对我来说似乎是一个限制。 我能够将TIFF图像文件读取为byte []但由于位图返回null,所以要转换字节[]或文件输入流

请让我知道将TIFF图像文件转换为位图的任何其他方法。

正如VERT9x所说,Android不支持TIFF文件。 但是您可以使用原生Android库(例如https://github.com/puelocesar/android-lib-magick)来回转换它们。 这是将jpg转换为tif的示例代码。 希望这可以帮助。

MagickImage Image = new MagickImage(new ImageInfo(IMAGE_PATH));
// Image = Image.scaleImage(800, 800);
Image.setImageFormat("tif");
Image.setFileName(str);
ImageInfo localImageInfo = new ImageInfo(str);
localImageInfo.setMagick("tif");
Image.writeImage(localImageInfo);
// store as tif
byte[] blob = Image.imageToBlob(localImageInfo);
FileOutputStream localFileOutputStream = new FileOutputStream(str);
localFileOutputStream.write(blob);
localFileOutputStream.close();

Android本身不支持TIFF文件。 检查支持的媒体格式列表。

如果要支持TIFF文件,则必须自己手动对其进行解码,或者找到适合您的库。

要解码一个简单的彩色tif文件,请按照以下步骤操作:

    String taginfo="";String strVal=""; //These are Tiff Tags

    FileInputStream fis;BufferedInputStream bis;DataInputStream dis;

    path=Environment.getExternalStorageDirectory().getPath();   
    path=path+"/DCIM"+"/fileName.tif";  //whatever is your file-name.tif

    try{
         fis=new FileInputStream(path);
         bis=new bufferedInputStream(fis);
         dis=new DataInputStream(bis);

         dis.skip(4);              //skip 4 byte marker of TIFF+endian
         ifd=myInt(dis.readInt()); //read the 4 byte IFD-location

         txt="TIFF-IFD: "; //txt is the final text displayed in textView
         txt=txt+ifd;
         dis.skip(ifd-8); 
         entries=myShort(dis.readShort()); //read in your endian-style  
         txt=txt+"\nNo.OfEntries="+entries;

         for(int i=0;i<=entries;i++)
           {    
            tag=myShort( dis.readShort() );
            taginfo=tagInfo(tag); //tagInfo function returns info associated 
                                   //with tag
            type=myShort( dis.readShort() );
            count=myInt( dis.readInt() );
            value=myInt( dis.readInt() ); 

            if(type==3)strVal="Value="; else strVal="Offset=";

            if( strVal.equals("Offset=") )
            {
                if( taginfo.equals("StripOffsets") )   //image is stored as
                    {stripAt=value;stripCount=count;}  // strips of rows

                if( taginfo.equals("StripBytes")   )
                    {stripBytesAt=value;}
            }

            if( taginfo.equals("width") ){cols=value;}

            if( taginfo.equals("length") ){rows=value;}

            txt=txt+"\ntag="+tag+"" + tagInfo(tag) + ",type=" + type +  
                    ",count=" + count + strVal + value;
           }
         dis.close();bis.close();fis.close(); 

        }catch(Exception e)     { txt = txt + "\nerror=" + e.toString(); }

    txt=txt+"\nNo.OfStrips="+stripCount+",array of strip locations at: 
        "+stripAt+" and array of bytesPerStrip at "+stripBytesAt ;

    extractBMP(); // a function you will define to combine all strips to 
                   //bitmap image
}


public int myShort(short sh)
{   int i;
    ByteBuffer shortBuff=ByteBuffer.allocate(4);
    shortBuff.order(ByteOrder.BIG_ENDIAN);shortBuff.putShort(sh);shortBuff.rewind();
    shortBuff.order(ByteOrder.LITTLE_ENDIAN);sh=shortBuff.getShort();
    if(sh<0)i=(int)(sh+32768); else i=(int)sh;
    return i;
}
public long myInt(int i)
{    long l=0L;
     ByteBuffer intBuff=ByteBuffer.allocate(4);
     intBuff.order(ByteOrder.BIG_ENDIAN);intBuff.putInt(i);intBuff.rewind();
     intBuff.order(ByteOrder.LITTLE_ENDIAN); i=intBuff.getInt();
     if(i<0)l=(long)(i+2147483648L);     else l=(long)i; 
     return l;
}
public String tagInfo(int tag)
{   int i=0;
    switch(tag)
    {case 256: i=0;break;case 257: i=1;break;case 258: i=2;break;case 259: i=3;break;case 262: i=4;break;case 266: i=5;break;
        case 273: i=6;break;case 277: i=7;break;case 278: i=8;break;case 279: i=9;break;case 282: i=10;break;case 283: i=11;break;
        case 284: i=12;break;case 296: i=13;break;case 1496: i=14;break;case 0: i=15;break;
    }
    return info[i];
}

如果您想查看完整的代码,请参见此处: 在Android Java代码中解码Tiff图像

尝试使用我的库。 它支持从tiff直接转换为jpg / png / bmp https://github.com/Beyka/Android-TiffBitmapFactory

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM