【问题标题】:How to Fetch the Geotag details of the captured image or stored image in Windows phone 8如何在 Windows phone 8 中获取捕获的图像或存储的图像的地理标记详细信息
【发布时间】:2014-01-15 01:58:29
【问题描述】:

我想从图像中获取有关地理位置的信息,如下图所示

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

            }
        }

这样我们就可以检测到拍摄图像的位置。请帮助找到这些属性。

【问题讨论】:

    标签: c# geolocation windows-phone exif exiflib


    【解决方案1】:

    这些答案似乎都不是完全有效和正确的。这是我使用this EXIF library 得出的结论,也可以作为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;
    }
    

    助手:

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

    感谢Igor's answer 的帮助方法和geedubb's 的主要方法。

    【讨论】:

      【解决方案2】:

      您需要从图像中读取EXIF 数据。

      您可以使用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
          //...
      }
      

      更新。代码改为使用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...
                      }
                  }
              }
          }
      

      【讨论】:

      • 当我向 exif 阅读器提供流时出现错误,请您建议
      • @ArjunSharma 尝试将流位置设置为 0。
      • @igrali 出现错误无法读取超出流末尾的内容。
      • @ArjunSharma 请试试我的更新。请注意,我无法对此进行测试,但希望应该足够接近您想要的?
      • @ArjunSharma 是否在设置中打开了位置 > 位置?
      【解决方案3】:

      我记得您从选择器获得的 PhotoResult 没有 GPS 信息。但是有一种解决方法可以在 WP8 上使用 GPS 拍摄照片。 根据http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394006(v=vs.105).aspx

      在 Windows Phone 8 上,如果用户接受使用相机拍摄任务拍摄的照片,照片会自动保存到手机的相机胶卷中。

      所以你需要做的是在 MediaLibrary 中拍摄最后一张照片,而不是使用 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();
          }
      }
      

      IsolatedStorage 中保存的 x.jpg 将包含 GPS 信息,您可以使用任何可以处理 EXIF 数据的库来获取它。

      【讨论】:

        【解决方案4】:

        在我的PhotoTimeline wp8 应用程序中,我使用了这个ExifLib 和以下代码

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

        辅助函数定义为

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

        【讨论】:

        • Kulam 我试过但没有罚款 readjpeg 功能可以请给我建议你用过的版本
        • 你在哪里看的?它在链接的 DLL 库中
        • @igorkulam 我已经下载了 ExifLib 版本 1.4.7 但没有找到这样的功能请建议您正在谈论哪个链接的 Dll 库
        • 存在一些混淆,因为有两个用于解析 Exif 元数据的独立库,同名 (ExifLib)。第一个:codeproject.com/Articles/47486/…。第二个:codeproject.com/Articles/36342/…。这个答案使用第一个。接受的答案似乎使用第二个。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多