【问题标题】:Save and Retrieve location inside JPEG image保存和检索 JPEG 图像内的位置
【发布时间】:2016-02-26 13:58:20
【问题描述】:

我将分享一个解决方案,其中包括在 JPEG 图像文件中保存和检索位置。纬度和经度是使用 ExifInterface 在图像元数据中保存和检索的 更多关于 ExifInterface 的信息可以在这里找到 http://developer.android.com/reference/android/media/ExifInterface.html

【问题讨论】:

    标签: android image image-processing location jpeg


    【解决方案1】:
    public void saveLocation() {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitude);
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitude);
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void retriveLocation() {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
            String[] latitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE).split(",");
            String[] longitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE).split(",");
            String[] tmp = new String[2];
            tmp = latitudeValue[0].split("/");
            setLatitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
            tmp = longitudeValue[0].split("/");
            setLongitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      使用ExifInterface,您可以从媒体获取以下信息。

      变量声明。

      String mediaDateTime,attrLATITUDE,attrLATITUDE_REF,attrLONGITUDE,attrLONGITUDE_REF,zip, city,state, country;;
      Double Latitude, Longitude;
      List<Address> addresses;
      Geocoder geocoder;
      

      用户ExifInterface使用以下方式。

      ExifInterface exifInterfaceMedia = new ExifInterface(<Your Image Path>);
      // This will give you data and time
      mediaDateTime = exifInterfaceMedia.getAttribute(ExifInterface.TAG_DATETIME);
      
      attrLATITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
      attrLATITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
      attrLONGITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
      attrLONGITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
      
      if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) {
        valid = true;
      
        if (attrLATITUDE_REF.equals("N")) {
          Latitude = convertToDegree(attrLATITUDE);
        } else {
          Latitude = 0 - convertToDegree(attrLATITUDE);
        }
        if (attrLONGITUDE_REF.equals("E")) {
          Longitude = convertToDegree(attrLONGITUDE);
        } else {
          Longitude = 0 - convertToDegree(attrLONGITUDE);
        }
      
        try {
          addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (addresses != null && addresses.size() > 0) {
          zip = addresses.get(0).getPostalCode();
          city = addresses.get(0).getLocality();
          state = addresses.get(0).getAdminArea();
          country = addresses.get(0).getCountryName();
          if (zip != null) {
            title += zip + ",";
          }
          if (city != null) {
            title += city + ",";
          }
          if (state != null) {
            title += state + ",";
          }
          if (country != null) {
            title += country;
          }
        } else {
          title = "Unknown Location";
        }
      }
      
      private Double convertToDegree(String stringDMS) {
        Double result = null;
        String[] DMS = stringDMS.split(",", 3);
      
        String[] stringD = DMS[0].split("/", 2);
        Double D0 = new Double(stringD[0]);
        Double D1 = new Double(stringD[1]);
        Double FloatD = D0 / D1;
      
        String[] stringM = DMS[1].split("/", 2);
        Double M0 = new Double(stringM[0]);
        Double M1 = new Double(stringM[1]);
        Double FloatM = M0 / M1;
      
        String[] stringS = DMS[2].split("/", 2);
        Double S0 = new Double(stringS[0]);
        Double S1 = new Double(stringS[1]);
        Double FloatS = S0 / S1;
      
        result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));
      
        return result;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        相关资源
        最近更新 更多