From 9d86adea705ab8ca7bc68d3542a4f2c62dd3725f Mon Sep 17 00:00:00 2001 From: Ivan Pusic <450140+ivpusic@users.noreply.github.com> Date: Tue, 4 May 2021 11:15:55 +0200 Subject: [PATCH] (android) float variant of latitude/longitude exif data on android --- .../ivpusic/imagepicker/ExifExtractor.java | 6 ++ .../ivpusic/imagepicker/GeoDegree.java | 65 +++++++++++++++++++ .../android/app/src/main/AndroidManifest.xml | 2 + package.json | 2 +- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 android/src/main/java/com/reactnative/ivpusic/imagepicker/GeoDegree.java diff --git a/android/src/main/java/com/reactnative/ivpusic/imagepicker/ExifExtractor.java b/android/src/main/java/com/reactnative/ivpusic/imagepicker/ExifExtractor.java index af64b6a22..7f3f4deba 100644 --- a/android/src/main/java/com/reactnative/ivpusic/imagepicker/ExifExtractor.java +++ b/android/src/main/java/com/reactnative/ivpusic/imagepicker/ExifExtractor.java @@ -26,6 +26,12 @@ static WritableMap extract(String path) throws IOException { ExifInterface exif = new ExifInterface(path); + GeoDegree geoDegree = new GeoDegree(exif); + if (geoDegree.getLatitude() != null && geoDegree.getLongitude() != null) { + exifData.putDouble("Latitude", geoDegree.getLatitude()); + exifData.putDouble("Longitude", geoDegree.getLongitude()); + } + for (String attribute : attributes) { String value = exif.getAttribute(attribute); exifData.putString(attribute, value); diff --git a/android/src/main/java/com/reactnative/ivpusic/imagepicker/GeoDegree.java b/android/src/main/java/com/reactnative/ivpusic/imagepicker/GeoDegree.java new file mode 100644 index 000000000..6250c16cd --- /dev/null +++ b/android/src/main/java/com/reactnative/ivpusic/imagepicker/GeoDegree.java @@ -0,0 +1,65 @@ +package com.reactnative.ivpusic.imagepicker; + +import android.media.ExifInterface; + +public class GeoDegree { + Float latitude; + Float longitude; + + public GeoDegree(ExifInterface exif) { + String attrLATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE); + String attrLATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); + String attrLONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); + String attrLONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); + + if ((attrLATITUDE != null) + && (attrLATITUDE_REF != null) + && (attrLONGITUDE != null) + && (attrLONGITUDE_REF != null)) { + + 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); + } + } + } + + private Float convertToDegree(String stringDMS) { + Float result = null; + String[] DMS = stringDMS.split(",", 3); + + String[] stringD = DMS[0].split("/", 2); + Double D0 = Double.valueOf(stringD[0]); + Double D1 = Double.valueOf(stringD[1]); + double FloatD = D0 / D1; + + String[] stringM = DMS[1].split("/", 2); + Double M0 = Double.valueOf(stringM[0]); + Double M1 = Double.valueOf(stringM[1]); + double FloatM = M0 / M1; + + String[] stringS = DMS[2].split("/", 2); + Double S0 = Double.valueOf(stringS[0]); + Double S1 = Double.valueOf(stringS[1]); + double FloatS = S0 / S1; + + result = (float) (FloatD + (FloatM / 60) + (FloatS / 3600)); + + return result; + } + + public Float getLatitude() { + return latitude; + } + + public Float getLongitude() { + return longitude; + } +} diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index d6c2b854e..c741c4f9a 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -4,6 +4,8 @@ + +