首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >EXIF PropertyTagGpsLatitude格式

EXIF PropertyTagGpsLatitude格式
EN

Stack Overflow用户
提问于 2018-04-21 08:09:40
回答 2查看 562关注 0票数 0

我正在以编程方式更新一堆扫描的jpg图像的exif数据。

我在更新大多数exif数据时没有遇到太多问题,但是我正在努力设置gps坐标

例如,我正在尝试保存纬度坐标,如下所示

代码语言:javascript
复制
PropertyItem PropertyTagGpsLatitude = srcImg.GetPropertyItem(2);
PropertyTagGpsLatitude.Value = System.Text.ASCIIEncoding.ASCII.GetBytes("42/1,5/1,33/1");
srcImg.SetPropertyItem(PropertyTagGpsLatitude);

根据它声明的文档

纬度表示为三个有理值,分别表示度、分和秒。表示度、分和秒时,格式为dd/1,mm/1,ss/1。使用度和分时,例如,分钟的小数位最多为两个小数位时,格式为dd/1,mmmm/100,0/1。

https://msdn.microsoft.com/en-us/library/windows/desktop/ms534416(v=vs.85).aspx

我能够设置其他gps相关的数据,例如,这是正确转换过来的

代码语言:javascript
复制
 PropertyItem PropertyTagGpsLatitudeRef = srcImg.GetPropertyItem(1);
 PropertyTagGpsLatitudeRef.Value = System.Text.ASCIIEncoding.ASCII.GetBytes("N");
 srcImg.SetPropertyItem(PropertyTagGpsLatitudeRef);

我没有得到任何异常,我只是在使用exif Pilot实用程序验证Exif数据,并且可以看到它不能正常工作

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-23 21:29:24

我终于想出了一个可行的解决方案。我加载了一个保存GPS信息的现有图像,以检查数据是如何构造的。最终我在这里想出了这个方法

代码语言:javascript
复制
 /// <summary>
 /// Prepares a byte array that hold EXIF latitude/longitude data
 /// </summary>
 /// <param name="degrees">There are 360° of longitude (180° E ↔ 180° W) and 180° of latitude (90° N ↔ 90° S)</param>
 /// <param name="minutes">Each degree can be broken into 60 minutes</param>
 /// <param name="seconds">Each minute can be divided into 60 seconds (”). For finer accuracy, fractions of seconds given by a decimal point are used. Seconds unit multiplied by 100. For example 33.77 seconds is passed as 3377. This gives adequate GPS precision</param>
 /// <returns></returns>
 private static byte[] FloatToExifGps(int degrees, int minutes, int seconds)
 {
      var secBytes = BitConverter.GetBytes(seconds);
      var secDivisor = BitConverter.GetBytes(100);
      byte[] rv = { (byte)degrees, 0, 0, 0, 1, 0, 0, 0, (byte)minutes, 0, 0, 0, 1, 0, 0, 0, secBytes[0], secBytes[1], 0, 0, secDivisor[0], 0, 0, 0 };
      return rv;
 }

并像这样使用它

代码语言:javascript
复制
 PropertyItem PropertyTagGpsLongitude = srcImg.GetPropertyItem(4);
 PropertyTagGpsLongitude.Value = FloatToExifGps(79, 48, 3377);
 srcImg.SetPropertyItem(PropertyTagGpsLongitude);

我希望这对将来的人有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2018-05-28 18:16:11

我最近遇到了同样的问题,并想分享对我有效的解决方案。像LongitudeDegreeNumerator等变量都有uint类型,并且预先计算过。

代码语言:javascript
复制
// Longitude: Build a whole property value
byte[] propertyTagGpsLongitude = new byte[24];
Buffer.BlockCopy(BitConverter.GetBytes(LongitudeDegreeNumerator), 0, propertyTagGpsLongitude, 0, 4);
Buffer.BlockCopy(BitConverter.GetBytes(LongitudeDegreeDenominator), 0, propertyTagGpsLongitude, 4, 4);
Buffer.BlockCopy(BitConverter.GetBytes(LongitudeMinuteNumerator), 0, propertyTagGpsLongitude, 8, 4);
Buffer.BlockCopy(BitConverter.GetBytes(LongitudeMinuteDenominator), 0, propertyTagGpsLongitude, 12, 4);
Buffer.BlockCopy(BitConverter.GetBytes(LongitudeSecondNumerator), 0, propertyTagGpsLongitude, 16, 4);
Buffer.BlockCopy(BitConverter.GetBytes(LongitudeSecondDenominator), 0, propertyTagGpsLongitude, 20, 4);

也许这对某些人是有用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49951142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档