C# 如何在Windows phone 8中获取捕获图像或存储图像的地理标记详细信息

C# 如何在Windows phone 8中获取捕获图像或存储图像的地理标记详细信息,c#,geolocation,windows-phone,exif,exiflib,C#,Geolocation,Windows Phone,Exif,Exiflib,我想从图像中获取有关地理位置的信息,如下图所示 void cam_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { Image cameraImage = new Image(); BitmapImage bImage = new BitmapImage();

我想从图像中获取有关地理位置的信息,如下图所示

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

            }
        }

void cam_已完成(对象发送器,照片结果e)
{
if(e.TaskResult==TaskResult.OK)
{
Image cameraImage=新图像();
BitmapImage bImage=新的BitmapImage();
bImage.SetSource(如ChosenPhoto);
cameraImage.Source=bImage;
e、 ChosenPhoto.Position=0;
ExifReader=新的ExifReader(e.ChosenPhoto);
双gpsLat,gpsLng;
reader.GetTagValue(ExifTags.GPSLatitude,
外接(板条)
reader.GetTagValue(ExifTags.GPSLongitude,
out gpsLng)
MessageBox.Show(gpsLat.ToString()+“”+gpsLng.ToString());
}
}

这样我们就可以检测出拍摄图像的位置。请帮助查找这些属性。

您需要从图像中读取
EXIF
数据

你可以用

在我的wp8应用程序中,我使用以下代码

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

我记得你从选择器获得的照片结果没有GPS信息。但是有一个解决方法可以在WP8上用GPS拍摄照片。 据

在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数据的库将其取出。

这些答案似乎都不是完全正确的。下面是我使用的方法,也可以作为

助手:

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

对于helper方法和main方法,都要归功于。

当我将流提供给exif reader时,它会给出错误,您可以建议吗please@ArjunSharma尝试将流位置设置为0。@igrali获取错误无法读取超出流末尾的内容。@ArjunSharma请尝试我的更新。请注意,我无法测试此功能,但希望它足够接近您想要的功能?@ArjunSharma是否在“设置”>“位置”中打开了位置?Kulam我已尝试但未使用readjpeg功能,请向我推荐您使用的版本。您查看了吗?它位于链接的DLL中lib@igorkulam我已经加载了ExifLib版本1.4.7,但没有发现这样的功能,即请建议您正在谈论的链接Dll库。这里有一些混淆,因为有两个单独的库用于解析Exif元数据,具有相同的名称(ExifLib)。第一个:。第二个:。这个答案使用第一个。公认的答案似乎使用了第二个。
// 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();
    }
}
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);
}