C#圖片處理之: 擷取數位相片的EXIF資訊(二)

來源:互聯網
上載者:User
 

還是邊看個執行個體邊聊吧,我們會討論一些常用的ExifPropertyTagID,並瞭解如何得到它們的值。請牢記,MSDN是很好的資源。

隨便開啟張我拍的照片,按上次說的方法掃描每一個Exif屬性項目。
第一項的ID是0x010F。查MSDN,發現是“Null-terminated character string that specifies the manufacturer of the equipment used to record the image.”,原來是相機的製造商啊。再一看,Type == 2,嗯,還真不矛盾,調用解Type==2的函數:
private static string GetValueOfType2(byte[] b)
{
 return System.Text.Encoding.ASCII.GetString(b);
}

於是得到這樣的文字:
EASTMAN KODAK COMPANY

沒錯,我的正是柯達。繼續往下看,第二項的ID是0x0110,老規矩MSDN,發現是“Null-terminated character string that specifies the model name or model number of the equipment used to record the image.”,原來是型號。Type還是2,繼續調用剛才的函數,結果就是:
KODAK Z7590 ZOOM DIGITAL CAMERA
完全正確,加十分。

接著往下走,看到有個ID==0x8827(ISO Speed)的類型是3。查MSDN得知是16位不帶正負號的整數。把Value[1]左移8位放在Value[0]之前就可以了。
private static string GetValueOfType3(byte[] b)
{
 f (b.Length != 2) return "無效的類型(3)";
 return Convert.ToUInt16(b[1] << 8 | b[0]).ToString();
}
我的值是80,沒錯。0xA002和0xA003一起描述了圖片的原始大小,也可用這個方法求出來。

Type==4和Type==3情況類似,只不過Type=4時表示一個32位的不帶正負號的整數。
private static string GetValueOfType4(byte[] b)
{
 if (b.Length != 4) return "無效的類型(4)";
 return Convert.ToUInt32(b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0]).ToString();
}
例子是ID==0x0202(JPEG Inter Length)

Type==5也比較多,例如曝光時間(0x829A)。
這種類型包含了兩個無符號的長整型數字。但處理起來也並不難。把8個位元組分成兩份,Value[0~3]是分母,Value[4~7]是分子。但同樣別忘了移位。
private static string GetValueOfType5(byte[] b)
{
 if (b.Length != 8) return "無效的類型(5)";

 UInt32 fm, fz;
 fm = 0;
        fz = 0;

        fz = Convert.ToUInt32(b[7] << 24 | b[6] << 16 | b[5] << 8 | b[4]);
        fm = Convert.ToUInt32(b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0]);

        return fm.ToString() + "/" + fz.ToString();
}
我的樣本照片曝光時間是1/500。同類型的比較常用的ID還有0x829D(F-Number),0x011A,0x011B(分別代表x/y方向上的解析度),0x9202(Lens aperture)等。

Type==7的比較亂,至少我的柯達圖片不能被用統一的方式解碼。0x9000(Exif版本),0xA000(Exif FPX版本)可以這麼求:
private static string GetValueOfType7A(byte[] b)
{
            string rtn = "";

            for (int i = 0; i < b.Length; i++)
            {

                rtn += ((char)b[i]).ToString();

            }

            return rtn;
}

但0x9101(Exif壓縮配置)需要這麼解碼才正確:
private static string GetValueOfType7B(byte[] b)
{
            string rtn = "";

            for (int i = 0; i < b.Length; i++)
            {

                rtn += b[i].ToString();

            }

            return rtn;
}

搞不懂,誰知道請告訴我。

最後看一下類型10。其實和類型5差不多,只不過類型10是有符號的,這裡也就不多羅嗦了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.