Xamarin.ios 将地理标记信息添加到图像并上载到服务器

Xamarin.ios 将地理标记信息添加到图像并上载到服务器,xamarin.ios,xamarin.android,mvvmcross,geotagging,Xamarin.ios,Xamarin.android,Mvvmcross,Geotagging,说明:截止日期前两天,我发现并经斯图尔特确认,使用TakePicture方法拍摄的照片不会保存地理位置。 有效: 1.这张照片是用TakePicture拍摄并保存的 2.lat和lng被检测并发送到每个平台实施 3.图片保存到实现后,文件名从核心发送 4.平台实现将地理标记包括在图片中 5.图像上传工作,发送地理标签 将地理标记添加到图片的代码: public bool SaveImageWithGpsTags(string fileName, double lat, double lng) {

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

有效:
1.这张照片是用TakePicture拍摄并保存的
2.lat和lng被检测并发送到每个平台实施
3.图片保存到实现后,文件名从核心发送
4.平台实现将地理标记包括在图片中
5.图像上传工作,发送地理标签

将地理标记添加到图片的代码:

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


更新:(立即上传!):
以下是发送图像的代码:

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);
if(client.DefaultRequestHeaders.CacheControl==null)
client.DefaultRequestHeaders.CacheControl=新的CacheControlHeaderValue();
client.DefaultRequestHeaders.CacheControl.NoCache=true;
client.DefaultRequestHeaders.CacheControl.NoStore=true;
字节[]图像字节;
var result=Mvx.Resolve().TryReadBinaryFile(imagePath,out imageBytes);
var fileContent=newbytearraycontent(imageBytes,0,imageBytes.Count());
var fileName=NewGuid()+“.jpg”;
const string reference=“picture”
fileContent.Headers.ContentType=MediaTypeHeaderValue.Parse(“多部分/表单数据”);
fileContent.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“表单数据”)
{
FileName=FileName,
名称=参考,
};
content.Add(fileContent);
添加(新的StringContent(Settings.UserId),“UserId”);
等待client.PostAsync(“某些服务器URL”,内容);

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


对不起,我不知道

请写下你自己问题的答案并接受它。