开发者

Extracting EXIF from JPEG

开发者 https://www.devze.com 2023-02-06 10:06 出处:网络
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):

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); 
开发者_StackOverflow社区

I get this error on the last line:

Specified cast is not valid.

Any suggestions?


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


Another option for retrieving GPS metadata from images is to use the MetadataExtractor library. It's available on NuGet. It supports Exif GPS data from JPEG files, along with a tonne of other metadata types and file types.

To access the GPS location, use the following code:

var directories = ImageMetadataReader.ReadMetadata(jpegFilePath);

var gps = directories.OfType<GpsDirectory>().FirstOrDefault();

if (gps != null)
{
    var location = gps.GetGeoLocation();

    if (location != null)
        Console.WriteLine("Lat {0} Lng {1}", location.Latitude, location.Longitude);
}

Here's example output from an iPhone 6.

0

精彩评论

暂无评论...
验证码 换一张
取 消