从 JPEG 中提取 EXIF

发布于 2024-10-13 11:09:40 字数 710 浏览 7 评论 0原文

我沉迷于从 JPEG 中读取 EXIF 数据。我以为这很容易做到。

到目前为止,我已经为我家人的在线图片库完成了以下步骤(使用 C#/ASP.Net 3.5):

  1. 上传包含 JPEG 的 ZIP 文件(来自我的 iPhone 4)
  2. 使用首选命名约定重命名 ZIP 文件中的 JPEG
  3. 将 JPEG 从 ZIP 文件提取到图像文件夹
  4. 调整图像大小以供各种用途(例如缩略图等)
  5. 将文件名和选定的类别 ID 保存到 SQL Server,以便我可以将两者关联起来以用于显示

目的从原始 JPEG 图像中提取纬度和经度,然后将这些值插入到插入文件名和类别 ID 的同一过程中的数据库中(步骤 # 5)。我需要这些值才能与 Google Maps API 配合使用。最简单的方法是什么?

更新:

ExifLib 看起来很棒,但是当我执行以下操作时:

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

我在最后一行收到此错误:

指定的演员阵容无效。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-10-20 11:09:40

为了将所有答案汇总在一起,这是完整的解决方案。

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

    }

}

输出:

    The picture was taken at 76.8593333333333,39.077

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
屋顶上的小猫咪 2024-10-20 11:09:40

从图像中检索 GPS 元数据的另一种选择是使用 MetadataExtractor 库。它可以在 NuGet 上使用。它支持来自 JPEG 文件的 Exif GPS 数据,以及大量其他元数据类型和文件类型。

要访问 GPS 位置,请使用以下代码:

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

以下是 iPhone 6

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文