簡體   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