gpt4 book ai didi

vb.net - 如何将经度和纬度转换为 GPS Exif 字节数组

转载 作者:行者123 更新时间:2023-12-02 10:02:40 25 4
gpt4 key购买 nike

我试图将纬度 = 8°50'34.46"和经度 = 125° 9'50.82"放入图像的 exif 文件中。我正在使用 vb.net。

将度数和分钟转换为字节时没有问题,因为它是整数,但是当我将具有十进制值的秒(34.46“)转换为字节时。它给出了不同的结果,例如 0.9856。

请帮助我如何将带有十进制值的数字转换为字节。

这里是代码:

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 doubleToByteArray(ByVal dbl As Double) As Byte()
Return BitConverter.GetBytes(Convert.ToDecimal(dbl))
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 = 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(doubleToByteArray(seconds), 0, result, 16, 4)
Array.Copy(intToByteArray(1), 0, result, 20, 4)

Return result
End Function

最佳答案

根据this规范、经度和纬度被编码为

PropertyTagTypeRational

Specifies that the value data member is an array of pairs of unsigned long integers. Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.

编码布局应为(总共 24 个字节)

Byte Offset   Length   Encoding   Field
0 4 uint Degrees Nominator
4 4 uint Degrees Denominator
8 4 uint Minutes Nominator
12 4 uint Minutes Denominator
16 4 uint Seconds Nominator
20 4 uint Seconds Denominator

鉴于您的输入使用的是整数度和分,而不是分数,通过使用 1 的值作为分母,您对这两个值的编码将可以正常工作。

对于作为浮点值的秒,情况并非如此。您必须使用分子和分母部分将其编码为有理数。

我不确定您想要的精度是多少,但考虑到您的 34.46 秒示例,乘以 1000 并使用 1000 作为分母似乎会超过足够好:

Dim secondsNominator = Math.Truncate(1000 * seconds)
Dim secondsDenoninator = 1000

那么你的编码函数就变成了:

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 secondsNominator = Math.Truncate(1000 * temp)
Dim secondsDenoninator = 1000

Dim result(24) As Byte

' Degrees (nominator, and 1 for denominator)
Array.Copy(intToByteArray(degrees), 0, result, 0, 4)
Array.Copy(intToByteArray(1), 0, result, 4, 4)

' Minutes (nominator, and 1 for denominator)
Array.Copy(intToByteArray(minutes), 0, result, 8, 4)
Array.Copy(intToByteArray(1), 0, result, 12, 4)

' Seconds (1000 for denominator: ms resolution)
Array.Copy(intToByteArray(secondsNominator), 0, result, 16, 4)
Array.Copy(intToByteArray(secondsDenominator), 0, result, 20, 4)

Return result
End Function

关于vb.net - 如何将经度和纬度转换为 GPS Exif 字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com