图像元数据中的经度始终为零
Longitude is always zero in image meta data
我正在使用 Xamarin 中的 MediaPicker 拍照。我启动地理定位服务,然后在拍摄照片后,我将图像的字节数组和位置信息发送到我自己的平台特定实现,以将位置信息添加到图像的元数据中。
然后我将图像保存为文件,然后通过电子邮件将其发送给自己,这样我就可以在外部应用程序 (Picasa) 中打开它,以确保 GPS 信息已正确存储。
我遇到的问题 运行 是纬度和海拔高度显示正常,但经度始终为零。我在我的应用程序中放置了断点,并验证了元数据设置正确并且所有信息都具有有效值。我对这里发生的事情一头雾水。
下面的一些代码可能是多余的或效率低下的,因为我一直在测试添加元数据的不同方法。我在我的应用程序中使用以下代码 iOS 实现此元数据添加方法:
public byte[] AddPositionInformation(byte[] bytes, SaleScribe.PCL.Services.Geolocation.Position position)
{
var data = NSData.FromArray(bytes);
UIKit.UIImage original = UIKit.UIImage.LoadFromData(data);
CGImageSource myImageSource = CGImageSource.FromData(original.AsJPEG());
var options = new CGImageDestinationOptions();
options.GpsDictionary = new CoreGraphics.CGImagePropertiesGps();
options.GpsDictionary.Latitude = (float)position.Latitude;
options.GpsDictionary.Longitude = (float)position.Longitude;
options.GpsDictionary.Altitude = (int)position.Altitude;
NSMutableData mutableData = new NSMutableData();
using(var dest = CGImageDestination.Create(mutableData, myImageSource.TypeIdentifier, 1, new CGImageDestinationOptions()))
{
dest.AddImage(myImageSource, (int)(myImageSource.ImageCount - 1), options);
dest.Close();
}
return (mutableData as NSData).ToArray();
}
在接收这个字节数组的函数中,我只是将字节数组直接写入文件。
非常感谢任何帮助。
谢谢!
对于可能感兴趣的任何人,我必须使用另一种方法才能使其正常工作,但潜在的问题是 GPS 纬度和经度需要一个 uint,因此 -94.xxx 经度无效。我需要加上纬度和经度的绝对值,然后根据原始有符号值添加适当的参考值。
这是对我有用的代码:
public byte[] AddPositionInformation(byte[] bytes, SaleScribe.PCL.Services.Geolocation.Position position)
{
var data = NSData.FromArray(bytes);
CGImageSource myImageSource = CGImageSource.FromData(data);
var ns = new NSDictionary();
var imageProperties = myImageSource.CopyProperties(ns, 0);
var gps = new NSMutableDictionary();
gps.SetValueForKey(NSObject.FromObject(System.Math.Abs(position.Latitude)), CGImageProperties.GPSLatitude);
gps.SetValueForKey(NSObject.FromObject(new NSString(position.Latitude < 0 ? "S" : "N")), CGImageProperties.GPSLatitudeRef);
gps.SetValueForKey(NSObject.FromObject(System.Math.Abs(position.Longitude)), CGImageProperties.GPSLongitude);
gps.SetValueForKey(NSObject.FromObject(new NSString(position.Longitude < 0 ? "W" : "E")), CGImageProperties.GPSLongitudeRef);
gps.SetValueForKey(NSObject.FromObject(position.Altitude), CGImageProperties.GPSAltitude);
gps.SetValueForKey(NSObject.FromObject(position.Altitude < 0 ? 1 : 0), CGImageProperties.GPSAltitudeRef);
var mutableDictionary = imageProperties.MutableCopy();
mutableDictionary.SetValueForKey(gps, CGImageProperties.GPSDictionary);
NSMutableData mutableData = new NSMutableData();
var dest = CGImageDestination.Create(mutableData, myImageSource.TypeIdentifier, 1);
dest.AddImage(myImageSource, 0, mutableDictionary as NSDictionary);
dest.Close();
return mutableData.ToArray();
}
我正在使用 Xamarin 中的 MediaPicker 拍照。我启动地理定位服务,然后在拍摄照片后,我将图像的字节数组和位置信息发送到我自己的平台特定实现,以将位置信息添加到图像的元数据中。
然后我将图像保存为文件,然后通过电子邮件将其发送给自己,这样我就可以在外部应用程序 (Picasa) 中打开它,以确保 GPS 信息已正确存储。
我遇到的问题 运行 是纬度和海拔高度显示正常,但经度始终为零。我在我的应用程序中放置了断点,并验证了元数据设置正确并且所有信息都具有有效值。我对这里发生的事情一头雾水。
下面的一些代码可能是多余的或效率低下的,因为我一直在测试添加元数据的不同方法。我在我的应用程序中使用以下代码 iOS 实现此元数据添加方法:
public byte[] AddPositionInformation(byte[] bytes, SaleScribe.PCL.Services.Geolocation.Position position)
{
var data = NSData.FromArray(bytes);
UIKit.UIImage original = UIKit.UIImage.LoadFromData(data);
CGImageSource myImageSource = CGImageSource.FromData(original.AsJPEG());
var options = new CGImageDestinationOptions();
options.GpsDictionary = new CoreGraphics.CGImagePropertiesGps();
options.GpsDictionary.Latitude = (float)position.Latitude;
options.GpsDictionary.Longitude = (float)position.Longitude;
options.GpsDictionary.Altitude = (int)position.Altitude;
NSMutableData mutableData = new NSMutableData();
using(var dest = CGImageDestination.Create(mutableData, myImageSource.TypeIdentifier, 1, new CGImageDestinationOptions()))
{
dest.AddImage(myImageSource, (int)(myImageSource.ImageCount - 1), options);
dest.Close();
}
return (mutableData as NSData).ToArray();
}
在接收这个字节数组的函数中,我只是将字节数组直接写入文件。
非常感谢任何帮助。
谢谢!
对于可能感兴趣的任何人,我必须使用另一种方法才能使其正常工作,但潜在的问题是 GPS 纬度和经度需要一个 uint,因此 -94.xxx 经度无效。我需要加上纬度和经度的绝对值,然后根据原始有符号值添加适当的参考值。
这是对我有用的代码:
public byte[] AddPositionInformation(byte[] bytes, SaleScribe.PCL.Services.Geolocation.Position position)
{
var data = NSData.FromArray(bytes);
CGImageSource myImageSource = CGImageSource.FromData(data);
var ns = new NSDictionary();
var imageProperties = myImageSource.CopyProperties(ns, 0);
var gps = new NSMutableDictionary();
gps.SetValueForKey(NSObject.FromObject(System.Math.Abs(position.Latitude)), CGImageProperties.GPSLatitude);
gps.SetValueForKey(NSObject.FromObject(new NSString(position.Latitude < 0 ? "S" : "N")), CGImageProperties.GPSLatitudeRef);
gps.SetValueForKey(NSObject.FromObject(System.Math.Abs(position.Longitude)), CGImageProperties.GPSLongitude);
gps.SetValueForKey(NSObject.FromObject(new NSString(position.Longitude < 0 ? "W" : "E")), CGImageProperties.GPSLongitudeRef);
gps.SetValueForKey(NSObject.FromObject(position.Altitude), CGImageProperties.GPSAltitude);
gps.SetValueForKey(NSObject.FromObject(position.Altitude < 0 ? 1 : 0), CGImageProperties.GPSAltitudeRef);
var mutableDictionary = imageProperties.MutableCopy();
mutableDictionary.SetValueForKey(gps, CGImageProperties.GPSDictionary);
NSMutableData mutableData = new NSMutableData();
var dest = CGImageDestination.Create(mutableData, myImageSource.TypeIdentifier, 1);
dest.AddImage(myImageSource, 0, mutableDictionary as NSDictionary);
dest.Close();
return mutableData.ToArray();
}