How to Fetch the Geotag details of the captured image or stored image in Windows phone 8

后端 未结 4 954
说谎
说谎 2021-02-04 13:30

I want to fetch the information from the image regarding the Geolocation as shown in the image below

相关标签:
4条回答
  • 2021-02-04 13:48

    You will need to read the EXIF data from the image.

    You can use a library such as this

    // 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. Code changed to use 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...
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-04 13:56

    I remember that the PhotoResult you got from the chooser does not have the GPS info. But there's a workaround to get the taken photo with GPS on WP8. According to http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394006(v=vs.105).aspx

    On Windows Phone 8, if the user accepts a photo taken with the camera capture task, the photo is automatically saved to the phone’s camera roll.

    So what you have to do is taking the last photo in MediaLibrary instead of using the 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();
        }
    }
    

    The x.jpg saved in IsolatedStorage will have the GPS info and you can get it out using any library which can handle EXIF data.

    0 讨论(0)
  • 2021-02-04 13:59

    None of these answers seemed to be completely working and correct. Here's what I came up with using this EXIF library, which is also available as a NuGet package.

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

    Helpers:

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

    Credit to Igor's answer for the helper method and geedubb's for the main method.

    0 讨论(0)
  • 2021-02-04 14:10

    In my PhotoTimeline wp8 app I use this ExifLib and the following code

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

    with the helper function defined as

    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);
    }
    
    0 讨论(0)
提交回复
热议问题