10

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

在此处输入图像描述

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

            }
        }

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

4

4 回答 4

6

这些答案似乎都不是完全有效和正确的。这是我使用这个 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);
}

归功于Igor对辅助方法的回答和geedubb对主要方法的回答。

于 2015-01-30T15:29:10.903 回答
4

您将需要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
    //...
}

更新。代码更改为使用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...
                }
            }
        }
    }
于 2013-12-27T13:11:35.460 回答
3

在我的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);
}
于 2013-12-31T11:48:46.747 回答
2

我记得您从选择器获得的 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 数据的库来获取它。

于 2014-01-03T02:18:43.820 回答