【问题标题】:Longitude is always zero in image meta data图像元数据中的经度始终为零
【发布时间】:2016-06-07 19:12:40
【问题描述】:

我在 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();
}

在接收此字节数组的函数中,我只是将字节数组直接写入文件。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 只是为了确定,你不是站在经度 0 度吗? :P
  • 哈哈,是的。我已经验证它是 -94.something

标签: ios xamarin.ios xamarin.forms xamarin-studio xamarin.mac


【解决方案1】:

对于任何可能感兴趣的人,我不得不使用另一种方法来使其工作,但根本问题是 GPS 纬度和经度需要一个 uint,因此 -94.xxx 经度无效。我需要添加 lat 和 long 的绝对值,然后根据原始有符号值添加适当的 ref 值。

这是对我有用的代码:

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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多