Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting EXIF from JPEG

I am getting hung up on reading EXIF data from my JPEGs. I thought it would be easy to do.

Thus far I have completed the following steps for my family's online image gallery (using C#/ASP.Net 3.5):

  1. Upload a ZIP file containing JPEG's (that are from my iPhone 4)
  2. Rename the JPEG's in the ZIP file using a preferred naming convention
  3. Extract the JPEG's from the ZIP file to an images folder
  4. Resize the images for various uses (such as thumbnails, etc.)
  5. Save the filename and a selected category ID to SQL Server so that I can associate the two for display purposes

I would like to extract the latitude and longitude from the original JPEG image and then insert those values into my database in the same proc that inserts the filename and category ID (step # 5). I need these values to work with the Google Maps API. What is the simplest way to do it?

Update:

ExifLib looks great, but when I do the following:

double d; 
ExifReader er = new ExifReader(sFileName); 
er.GetTagValue<double>(ExifTags.GPSLatitude, out d); 

I get this error on the last line:

Specified cast is not valid.

Any suggestions?

like image 677
Yoav Avatar asked Jan 23 '11 03:01

Yoav


People also ask

How do I view EXIF data on a JPEG?

Viewing EXIF data in Windows is easy. Just right-click on the photo in question and select “Properties”. Click on the “Details” tab and scroll down—you'll see all kinds of information about the camera used, and the settings the photo was taken with.

Does JPEG have EXIF data?

The exchangeable image file format (EXIF) is a standard for embedding technical metadata in image files that many camera manufacturers use and many image-processing programs support. EXIF metadata can be embedded in TIFF and JPEG images.


1 Answers

To bring all the answers together, here is the completed solution.

using (ExifReader reader = new ExifReader(e.Target))
{
    Double[] GpsLongArray;
    Double[] GpsLatArray;
    Double GpsLongDouble;
    Double GpsLatDouble;

    if (reader.GetTagValue<Double[]>(ExifTags.GPSLongitude, out GpsLongArray) 
        && reader.GetTagValue<Double[]>(ExifTags.GPSLatitude, out GpsLatArray))
    {
        GpsLongDouble = GpsLongArray[0] + GpsLongArray[1] / 60 + GpsLongArray[2] / 3600;
        GpsLatDouble  = GpsLatArray[0]  + GpsLatArray[1]  / 60 + GpsLatArray[2]  / 3600;

        Console.WriteLine("The picture was taken at {0},{1}", GpsLongDouble, GpsLatDouble);

    }

}

Output:

    The picture was taken at 76.8593333333333,39.077
like image 142
Orwellophile Avatar answered Sep 27 '22 21:09

Orwellophile