从图片的EXIF获取GPS数据在C# [英] Getting GPS data from an image's EXIF in C#

查看:308
本文介绍了从图片的EXIF获取GPS数据在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个系统,它允许为图像被上传到使用ASP.NET C#中的服务器。我处理图像和所有工作很大。我设法找到读取创建日期EXIF数据的方法和我解析它作为一个日期时间。这工作太伟大。

I am developing a system that allows for an image to be uploaded to a server using ASP.NET C#. I am processing the image and all is working great. I have managed to find a method that reads the Date Created EXIF data and am parsing it as a DateTime. That works great too.

我现在试图从EXIF读取GPS数据。我想捕捉纬度和经度数字。

I am now trying to read GPS data from the EXIF. I am wanting to capture the Latitude and Longitude figures.

我使用这个列表作为对EXIF数据的引用(使用该属性物品的数量)的 http://www.exiv2.org/tags.html

I am using this list as a reference to the EXIF data (using the numbers for the property items) http://www.exiv2.org/tags.html

下面是捕捉创建日期(工作)的方法。

Here is the method to capture the date created (which works).

public DateTime GetDateTaken(Image targetImg)
{
    DateTime dtaken;

    try
    {
        //Property Item 306 corresponds to the Date Taken
        PropertyItem propItem = targetImg.GetPropertyItem(0x0132);

        //Convert date taken metadata to a DateTime object
        string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
        string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
        string firsthalf = sdate.Substring(0, 10);
        firsthalf = firsthalf.Replace(":", "-");
        sdate = firsthalf + secondhalf;
        dtaken = DateTime.Parse(sdate);
    }
    catch
    {
        dtaken = DateTime.Parse("1956-01-01 00:00:00.000");
    }
    return dtaken;
}



下面是我在做同样的GPS ..尝试

Below is my attempt at doing the same for GPS..

public float GetLatitude(Image targetImg)
{
    float dtaken;

    try
    {
        //Property Item 0x0002 corresponds to the Date Taken
        PropertyItem propItem = targetImg.GetPropertyItem(2);

        //Convert date taken metadata to a DateTime object
        string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
        dtaken = float.Parse(sdate);
    }
    catch
    {
        dtaken = 0;
    }
    return dtaken;
}

这是走出来,进入SDATE值为5\0\ 0\0\0\0\0l\t\0\0d\0\0\0\0\0\0\0\0\0\\ \\0

The value that's coming out and into sdate is "5\0\0\0\0\0\0l\t\0\0d\0\0\0\0\0\0\0\0\0\0"

这是从被采取由不携带GPS EXIF​​数据的iPhone 4的图像来了。

And that is coming from an image that was taken by an iPhone 4 which does carry the GPS EXIF data.

我知道有课在那里,这样做,但宁愿写我自己 - 我愿意接受建议,但: - )

I know there are classes out there that do this but would prefer to write my own - I am open to suggestions though :-)

在此先感谢

推荐答案

据该的上面的链接通过tomfanning 发布,物业项目为0x0002表示为 PropertyTagTypeRational 纬度。理性的类型定义为 ...

According to the link posted above by tomfanning, property item 0x0002 is the latitude expressed as a PropertyTagTypeRational. The rational type is defined as...

指定值数据成员是对无符号长整数阵列>。每一对代表一个部分;第一个整数是分子,第二个整数是分母。

Specifies that the value data member is an array of pairs of unsigned long integers. Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.

您正试图解析它作为一个字符串时,它实际上只是一系列字节。根据上述情况,有应该是3对32位无符号整数打包成字节数组,您可以检索使用以下内容:

You are trying to parse it as a string when it's actually just a series of bytes. According to the above, there should be 3 pairs of 32-bit unsigned integers packed into that byte array, which you can retrieve using the following:

uint degreesNumerator   = BitConverter.ToUInt32(propItem.Value, 0);
uint degreesDenominator = BitConverter.ToUInt32(propItem.Value, 4);
uint minutesNumerator   = BitConverter.ToUInt32(propItem.Value, 8);
uint minutesDenominator = BitConverter.ToUInt32(propItem.Value, 12);
uint secondsNumerator   = BitConverter.ToUInt32(propItem.Value, 16);
uint secondsDenominator = BitConverter.ToUInt32(propItem.Value, 20);



你做什么这些值,你已经得到了他们之后是为您的工作了:)这里的什么文件说:

What you do with these values after you've got them is for you to work out :) Here's what the docs say:

纬度表示三个合理值分别给予度,分,秒。当度,分,秒来表示,格式为DD / 1毫米/ 1,SS / 1。当度和分使用,例如,分的级分给予最多两个小数位,则格式将DD / 1,MMMM / 100,0/1

Latitude is expressed as three rational values giving the degrees, minutes, and seconds respectively. When degrees, minutes, and seconds are expressed, the format is dd/1, mm/1, ss/1. When degrees and minutes are used and, for example, fractions of minutes are given up to two decimal places, the format is dd/1, mmmm/100, 0/1.

这篇关于从图片的EXIF获取GPS数据在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆