2012-07-19 61 views
0

我在將GPS座標轉換爲可作爲EXIF信息存儲的字節數組時遇到問題。將Lat/Long GPS座標轉換爲EXIF Rational字節數組

This questions表明EXIF座標應該表示爲三個有理數:degrees/1, minutes/1, seconds/1。我毫不費力地將小數座標轉換爲該座標。例如42.1234567很容易轉換爲42/1, 7/1, 24/1

我的問題是,我不明白如何將它表示爲一個字節數組,當我將它寫入圖像EXIF信息。我使用的庫叫做ExifWorks,我在VB.NET中使用它。

ExifWorks setProperty方法有三件事:EXIF字段ID,一個字節數組作爲數據,以及數據類型。下面是我如何使用它:

ew.SetProperty(TagNames.GpsLatitude, byteArrayHere, ExifWorks.ExifDataTypes.UnsignedRational) 

我也試過:

ew.SetPropertyString(TagNames.GpsLatitude, "42/1, 7/1, 24/1") 

這也不起作用。

所以,我的問題是,如何將度數分秒座標轉換爲字節數組?到目前爲止,我所嘗試過的所有內容最終都會成爲無效的EXIF信息,並且不起作用。一般的解決方案很好......不一定要在VB.net中工作。

+0

你或許應該得到的Exif規格(這是在網上公佈),看看GPS標籤的定義。然後得到一個十六進制編輯器,看看你實際上在寫什麼文件。另一個有用的工具是exiftool。有趣的是,exiftool顯示了您之前鏈接的JPEG文件的正確GPS數據。 – 2012-07-19 23:12:58

回答

0

我已經想通了。這裏的解決方案:

Private Shared Function intToByteArray(ByVal int As Int32) As Byte() 
    ' a necessary wrapper because of the cast to Int32 
    Return BitConverter.GetBytes(int) 
End Function 

Private Shared Function doubleCoordinateToRationalByteArray(ByVal doubleVal As Double) As Byte() 
    Dim temp As Double 

    temp = Math.Abs(doubleVal) 
    Dim degrees = Math.Truncate(temp) 

    temp = (temp - degrees) * 60 
    Dim minutes = Math.Truncate(temp) 

    temp = (temp - minutes) * 60 
    Dim seconds = Math.Truncate(temp) 

    Dim result(24) As Byte 
    Array.Copy(intToByteArray(degrees), 0, result, 0, 4) 
    Array.Copy(intToByteArray(1), 0, result, 4, 4) 
    Array.Copy(intToByteArray(minutes), 0, result, 8, 4) 
    Array.Copy(intToByteArray(1), 0, result, 12, 4) 
    Array.Copy(intToByteArray(seconds), 0, result, 16, 4) 
    Array.Copy(intToByteArray(1), 0, result, 20, 4) 

    Return result 
End Function 
0

你會得到更好的精度(0.001弧秒,這是一英寸)​​做

 Dim milliseconds = Math.Truncate(temp* 1000.0) 

    ... 

    Array.Copy(intToByteArray(milliseconds), 0, result, 16, 4) 
    Array.Copy(intToByteArray(1000), 0, result, 20, 4)