将Geo标记信息添加到图像并上传到服务器

时间:2014-02-08 21:03:53

标签: xamarin.ios xamarin.android mvvmcross geotagging

说明:在截止日期前两天,我发现并通过Stuart确认使用TakePicture方法拍摄的照片不会保存地理位置。

WORKS:
   1.照片是用TakePicture拍摄的,然后保存了    2.检测lat和lng并将其发送到每个平台实现
   3.图片保存到实现后,fileName从核心发送    4.平台实现将Geo Tags包含在图片中    5.图片上传工作,发送地理标签

将GEO标记为图片的代码:

ANDROID(更新:工作):

public bool SaveImageWithGpsTags(string fileName, double lat, double lng)
{
   var context = Mvx.Resolve<IMvxAndroidGlobals>().ApplicationContext;
   var fullPath = Path.Combine(context.FilesDir.Path, fileName);

   if (!File.Exists(fullPath)) return false;
   try
   {
       using (var ef = new ExifInterface(fullPath))
       {
          ef.SetAttribute(ExifInterface.TagGpsLatitude, Dec2Dms(lat));
          ef.SetAttribute(ExifInterface.TagGpsLongitude, Dec2Dms(lng));
          ef.SetAttribute(ExifInterface.TagGpsLatitudeRef, lat > 0 ? "N" : "S");
          ef.SetAttribute(ExifInterface.TagGpsLongitudeRef, lng > 0 ? "E" : "W");
          ef.SaveAttributes();
       }
   }
   catch (Exception e)
   {
       return false;
   }
}

static String Dec2Dms(double coord)
{
    coord = coord > 0 ? coord : -coord;  // -105.9876543 -> 105.9876543
    var sOut = string.Format("{0}/1,", ((int)coord));   // 105/1,
    coord = (coord % 1) * 60;         // .987654321 * 60 = 59.259258
    sOut = sOut + string.Format("{0}/1,", ((int)coord));   // 105/1,59/1,
    coord = (coord % 1) * 60000;             // .259258 * 60000 = 15555
    sOut = sOut + string.Format("{0}/1000,", ((int)coord));   // 105/1,59/1,15555/1000
    return sOut;
}

接触(更新:工作):

   const string resScheme = "res:";
   var imagePath = fileName.StartsWith(resScheme) ? fileName.Substring(resScheme.Length) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
   var originalImage = UIImage.FromFile(imagePath);
   var gpsDict = new NSMutableDictionary();
   var imageMetadata = new NSMutableDictionary();

   gpsDict.SetValueForKey(NSObject.FromObject(lng), new NSString("Longitude"));
   gpsDict.SetValueForKey(NSObject.FromObject(lng > 0 ? "E" : "W"), new NSString("LongitudeRef"));
   gpsDict.SetValueForKey(NSObject.FromObject(lat), new NSString("Latitude"));
   gpsDict.SetValueForKey(NSObject.FromObject(lat > 0 ? "N" : "S"), new NSString("LatitudeRef"));
   gpsDict.SetValueForKey(NSObject.FromObject(DateTime.UtcNow.ToString("HH:MM:ss.ff")), new NSString("TimeStamp"));
   imageMetadata.SetValueForKey (gpsDict as NSDictionary, MonoTouch.ImageIO.CGImageProperties.GPSDictionary);

   var imgSrc = CGImageSource.FromData (originalImage.AsJPEG ());
   var outImageData = new NSMutableData();

   using (
        var d = CGImageDestination.FromData(outImageData, imgSrc.TypeIdentifier, 1,
                new CGImageDestinationOptions()))
        {
            d.AddImage (imgSrc, imgSrc.ImageCount - 1, imageMetadata);
            d.Close();
        }
  NSError writeError;
  var imageSaved = outImageData.Save(imagePath, NSDataWritingOptions.Atomic, out writeError);


<小时/> 更新:(上传工作现在!):
以下是发送图像的代码:

if (client.DefaultRequestHeaders.CacheControl == null)
    client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue();

client.DefaultRequestHeaders.CacheControl.NoCache = true;
client.DefaultRequestHeaders.CacheControl.NoStore = true;

byte[] imageBytes;
var result = Mvx.Resolve<IMvxFileStore>().TryReadBinaryFile(imagePath, out imageBytes);
var fileContent = new ByteArrayContent(imageBytes,0,imageBytes.Count());
var fileName = NewGuid() + ".jpg";
const string reference = "picture"
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
    FileName = fileName,
    Name = reference,
};
content.Add(fileContent);
content.Add(new StringContent(Settings.UserId), "userid");
await client.PostAsync("SOME SERVER URL", content);

1 个答案:

答案 0 :(得分:0)

对解决方案感兴趣的每个人都会在问题本身更新后找到答案!

抱歉Cheesebaron,我不知道!