Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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

            }
        }

so that we can detect location where the image was taken. Please help to find the these property.

like image 871
Arjun Sharma Avatar asked Dec 27 '13 13:12

Arjun Sharma


People also ask

How do I get geotagging info from a picture?

Find the GPS Coordinates In Windows, all you have to do is right-click a picture file, select “Properties,” and then click the “Details” tab in the properties window. Look for the Latitude and Longitude coordinates under GPS.

How do I find the location of an image in Windows?

In the Windows Photos application, you can access image location data by selecting the three black dots in the top right corner. Now, select File info. Here you'll find image metadata, including a map with a location indicator. Select Open map to view the marked image location in Microsoft Maps.


4 Answers

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.

like image 94
Sam Avatar answered Oct 12 '22 15:10

Sam


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...
                }
            }
        }
    }
like image 27
geedubb Avatar answered Oct 12 '22 13:10

geedubb


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);
}
like image 32
Igor Kulman Avatar answered Oct 12 '22 15:10

Igor Kulman


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.

like image 28
huntert Avatar answered Oct 12 '22 13:10

huntert