描述:在截止日期前两天,我发现并由 Stuart 确认使用 TakePicture 方法拍摄的照片不会保存地理位置。
WORKS:
1. 使用 TakePicture 拍摄图片并保存
2. 检测 lat 和 lng 并将其发送到每个平台实现
3. 将图片保存到实现后从核心发送文件名
4. 平台实现包括 Geo标签入图片
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;
}
触摸(更新:工作):
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);