【问题标题】:Is possible to get Image GPS location coordinates from the Image?是否可以从图像中获取图像 GPS 位置坐标?
【发布时间】:2019-07-30 07:05:06
【问题描述】:

我正在构建一个 Flutter 应用程序,用户可以在其中发布照片及其位置。用户既可以从相机中获取图片,也可以从库中获取图片。

如果用户从相机拍照,我可以使用设备的 GPS 位置来设置照片的位置。我正在尝试根据其元数据获取图片的 GSP 位置,但是,我还没有找到方法。

有可能吗?如何最好的方法?

编辑:我在问题中添加了“GPS”以明确表示。我试图找出拍摄照片的物理位置。我还在问题中添加了“Flutter”,以表明我正面临在 Flutter App 中解决此问题的问题。

【问题讨论】:

    标签: flutter geolocation


    【解决方案1】:

    如果图像是 JPEG 或 RAW 图像文件,地理位置元数据将存储为 Exif 标签。在 Android 中,这是通过 ExifInterface 类完成的。

    标签是TAG_GPS_LATITUDE 和TAG_GPS_LONGITUDE: https://developer.android.com/reference/android/media/ExifInterface.html

    请注意坐标需要用有理数格式表示:度、分、秒,如dd/1,mm/1,ss/1。

    【讨论】:

    • 我尝试了类似的方法,但无法获取此 Exif 标签。也许我正在使用的 ImagePicker 无法检索或限制标签。你用 Flutter 做过这个吗?
    【解决方案2】:

    我发现我之前的问题与设备的相机权限有关。通常来自 android 的相机会访问用户位置并将 GPS 坐标保存为 Exif 标签,但 ios 设备通常不会默认保存,除非用户允许相机应用的位置权限。

    我更改了我的应用程序以检查图像是否具有 GPS 坐标,并且用户决定是否要分享他的实际位置或图像位置。

    我还需要做一些数学运算来处理坐标:

    为了使用代码,您必须将包 exif 和 geoflutterfire 添加到您的应用程序中。

    void _checkGPSData() async {
        Map<String, IfdTag> imgTags = await readExifFromBytes( File(image.path).readAsBytesSync() );
    
        if (imgTags.containsKey('GPS GPSLongitude')) {
          setState(() {
            _imgHasLocation = true;
            _imgLocation = exifGPSToGeoFirePoint(imgTags);
          });
        }
    
      }
    
    
    GeoFirePoint exifGPSToGeoFirePoint(Map<String, IfdTag> tags) {
    
      final latitudeValue = tags['GPS GPSLatitude'].values.map<double>( (item) => (item.numerator.toDouble() / item.denominator.toDouble()) ).toList();
      final latitudeSignal = tags['GPS GPSLatitudeRef'].printable;
    
    
      final longitudeValue = tags['GPS GPSLongitude'].values.map<double>( (item) => (item.numerator.toDouble() / item.denominator.toDouble()) ).toList();
      final longitudeSignal = tags['GPS GPSLongitudeRef'].printable;
    
      double latitude = latitudeValue[0]
        + (latitudeValue[1] / 60)
        + (latitudeValue[2] / 3600);
    
      double longitude = longitudeValue[0]
        + (longitudeValue[1] / 60)
        + (longitudeValue[2] / 3600);
    
      if (latitudeSignal == 'S') latitude = -latitude;
      if (longitudeSignal == 'W') longitude = -longitude;
    
      return  GeoFirePoint(latitude, longitude);
    }
    

    【讨论】:

    • 我也有这个问题,我已经允许位置和相机权限,但仍然无法从照片中获取任何 GPS 信息:(
    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    相关资源
    最近更新 更多