[英]How to Fetch the Geotag details of the captured image or stored image in Windows phone 8
我想从图像中获取有关地理位置的信息 ,如下图所示
void cam_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
Image cameraImage = new Image();
BitmapImage bImage = new BitmapImage();
bImage.SetSource(e.ChosenPhoto);
cameraImage.Source = bImage;
e.ChosenPhoto.Position = 0;
ExifReader reader = new ExifReader(e.ChosenPhoto);
double gpsLat, gpsLng;
reader.GetTagValue<double>(ExifTags.GPSLatitude,
out gpsLat))
reader.GetTagValue<double>(ExifTags.GPSLongitude,
out gpsLng))
MessageBox.Show(gpsLat.ToString() + "" + gpsLng.ToString());
}
}
这样我们就可以检测拍摄图像的位置。 请帮助找到这些属性。
这些答案似乎都没有完全正常和正确。 以下是我使用这个EXIF库的想法,它也可以作为NuGet包使用 。
public static double[] GetLatLongFromImage(string imagePath)
{
ExifReader reader = new ExifReader(imagePath);
// EXIF lat/long tags stored as [Degree, Minute, Second]
double[] latitudeComponents;
double[] longitudeComponents;
string latitudeRef; // "N" or "S" ("S" will be negative latitude)
string longitudeRef; // "E" or "W" ("W" will be a negative longitude)
if (reader.GetTagValue(ExifTags.GPSLatitude, out latitudeComponents)
&& reader.GetTagValue(ExifTags.GPSLongitude, out longitudeComponents)
&& reader.GetTagValue(ExifTags.GPSLatitudeRef, out latitudeRef)
&& reader.GetTagValue(ExifTags.GPSLongitudeRef, out longitudeRef))
{
var latitude = ConvertDegreeAngleToDouble(latitudeComponents[0], latitudeComponents[1], latitudeComponents[2], latitudeRef);
var longitude = ConvertDegreeAngleToDouble(longitudeComponents[0], longitudeComponents[1], longitudeComponents[2], longitudeRef);
return new[] { latitude, longitude };
}
return null;
}
助手:
public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds, string latLongRef)
{
double result = ConvertDegreeAngleToDouble(degrees, minutes, seconds);
if (latLongRef == "S" || latLongRef == "W")
{
result *= -1;
}
return result;
}
public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds)
{
return degrees + (minutes / 60) + (seconds / 3600);
}
您需要从图像中读取EXIF
数据。
您可以使用此类库
// Instantiate the reader
ExifReader reader = new ExifReader(@"..path to your image\...jpg");
// Extract the tag data using the ExifTags enumeration
double gpsLat, gpsLng;
if (reader.GetTagValue<double>(ExifTags.GPSLatitude,
out gpsLat))
{
// Do whatever is required with the extracted information
//...
}
if (reader.GetTagValue<double>(ExifTags.GPSLongitude,
out gpsLng))
{
// Do whatever is required with the extracted information
//...
}
UPDATE。 代码已更改为使用MemoryStream
void cam_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
using (MemoryStream memo = new MemoryStream())
{
e.ChosenPhoto.CopyTo(memo);
memo.Position = 0;
using (ExifReader reader = new ExifReader(memo))
{
double[] latitudeComponents;
reader.GetTagValue(ExifTags.GPSLatitude, out latitudeComponents);
double[] longitudeComponents;
reader.GetTagValue(ExifTags.GPSLongitude, out longitudeComponents);
// Lat/long are stored as D°M'S" arrays, so you will need to reconstruct their values as below:
var latitude = latitudeComponents[0] + latitudeComponents[1] / 60 + latitudeComponents[2] / 3600;
var longitude = longitudeComponents[0] + longitudeComponents[1] / 60 + longitudeComponents[2] / 3600;
// latitude and longitude should now be set correctly...
}
}
}
}
在我的PhotoTimeline wp8应用程序中,我使用此ExifLib和以下代码
var info = ExifReader.ReadJpeg(stream, picture.Name);
latitude = Utils.ConvertDegreeAngleToDouble(info.GpsLatitude[0], info.GpsLatitude[1], info.GpsLatitude[2], info.GpsLatitudeRef);
longitude = Utils.ConvertDegreeAngleToDouble(info.GpsLongitude[0], info.GpsLongitude[1], info.GpsLongitude[2], info.GpsLongitudeRef);
将辅助函数定义为
public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds, ExifGpsLatitudeRef exifGpsLatitudeRef)
{
double result = ConvertDegreeAngleToDouble(degrees, minutes, seconds);
if (exifGpsLatitudeRef == ExifGpsLatitudeRef.South)
{
result = -1*result;
}
return result;
}
public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds)
{
return degrees + (minutes/60) + (seconds/3600);
}
我记得你从选择器得到的PhotoResult没有GPS信息。 但是有一种解决方法可以在WP8上使用GPS拍摄照片。 根据http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394006(v=vs.105).aspx
在Windows Phone 8上,如果用户接受使用相机拍摄任务拍摄的照片,则照片会自动保存到手机的相机胶卷中。
所以你要做的就是在MediaLibrary中拍摄最后一张照片,而不是使用PhotoResult。
// For WP8, the taken photo inside a app will be automatically saved.
// So we take the last picture in MediaLibrary.
using (MediaLibrary library = new MediaLibrary())
{
string filePath = "x.jpg";
MemoryStream fileStream = new MemoryStream();// MemoryStream does not need to call Close()
Picture photoFromLibrary = library.Pictures[library.Pictures.Count - 1];// Get last picture
Stream stream = photoFromLibrary.GetImage();
stream.CopyTo(fileStream);
SaveMemoryStream(fileStream, filePath);
}
private void SaveMemoryStream(MemoryStream ms, string path)
{
try
{
using (var isolate = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream file = new IsolatedStorageFileStream(path, FileMode.Create, FileAccess.Write, isolate))
{
ms.WriteTo(file);
}
}
}
finally
{
IsolatedStorageMutex.ReleaseMutex();
}
}
保存在IsolatedStorage中的x.jpg将具有GPS信息,您可以使用任何可以处理EXIF数据的库来获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.