Skip to content

Commit

Permalink
Fix extraction of cardinal direction information
Browse files Browse the repository at this point in the history
Explicitly extracting the exif information stopped working, however the
current revision of ExifInterface allowed us to migrate to that for
exverything.
  • Loading branch information
simonpoole committed Aug 10, 2023
1 parent 07b44f9 commit 9a04331
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 166 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ dependencies {
implementation "com.google.android.material:material:1.3.0"
implementation "androidx.annotation:annotation:1.1.0"
implementation "androidx.core:core:1.7.0"
implementation "androidx.exifinterface:exifinterface:1.1.0"
implementation "androidx.exifinterface:exifinterface:1.3.6"
implementation "androidx.legacy:legacy-preference-v14:1.0.0"
implementation "androidx.work:work-runtime:$work_version"
implementation "androidx.window:window:1.0.0"
Expand All @@ -614,8 +614,6 @@ dependencies {
implementation 'se.akerfeldt:okhttp-signpost:1.1.0'
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
implementation "com.adobe.xmp:xmpcore:6.1.11"
implementation "com.drewnoakes:metadata-extractor:2.16.0"
implementation 'com.google.protobuf:protobuf-java:3.12.2'
implementation "com.google.code.gson:gson:2.8.9"
implementation "com.google.protobuf:protobuf-java:$googleProtobufVersion"
Expand Down
3 changes: 2 additions & 1 deletion src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<uses-sdk
android:minSdkVersion="14"
Expand Down
16 changes: 16 additions & 0 deletions src/androidTest/java/de/blau/android/photos/PhotosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,23 @@ public void setup() {
*/
@After
public void teardown() {
LayerUtils.removeLayer(main, LayerType.PHOTO);
map.setUpLayers(main);
map.invalidate();
PreferenceManager.getDefaultSharedPreferences(main).edit().putBoolean(main.getString(R.string.config_indexMediaStore_key), false).commit();
try (PhotoIndex index = new PhotoIndex(main)) {
RTree<Photo> tree = new RTree<>(2, 5);
index.fill(tree);
List<Photo> photos = new ArrayList<>();
tree.query(photos);
for (Photo p : photos) {
try {
main.getContentResolver().delete(p.getRefUri(main), null, null);
} catch (SecurityException ex) {
//
}
}
}
if (photo1 != null) {
photo1.delete();
}
Expand Down
47 changes: 36 additions & 11 deletions src/main/java/de/blau/android/photos/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

import android.content.Context;
Expand All @@ -14,7 +15,6 @@
import de.blau.android.R;
import de.blau.android.osm.BoundingBox;
import de.blau.android.osm.GeoPoint;
import de.blau.android.util.ExtendedExifInterface;
import de.blau.android.util.Util;
import de.blau.android.util.rtree.BoundedObject;

Expand Down Expand Up @@ -50,7 +50,28 @@ public class Photo implements BoundedObject, GeoPoint {
* @throws NumberFormatException If there was a problem parsing the XML.
*/
public Photo(@NonNull Context context, @NonNull Uri uri, @Nullable String displayName) throws IOException, NumberFormatException {
this(new ExtendedExifInterface(context, uri), uri.toString(), displayName);
this(new ExifInterface(openInputStream(context, uri)), uri.toString(), displayName);
}

/**
* Get an InputStream from an uri
*
* @param context Android context
* @param uri a content or file uri
* @return an InputStream
* @throws IOException if anything goes wrong
*/
@NonNull
private static InputStream openInputStream(@NonNull Context context, @NonNull Uri uri) throws IOException {
try {
return context.getContentResolver().openInputStream(uri);
} catch (Exception ex) {
// other stuff broken ... for example ArrayIndexOutOfBounds
throw new IOException(ex.getMessage());
} catch (Error err) { // NOSONAR crashing is not an option
// other stuff broken ... for example NoSuchMethodError
throw new IOException(err.getMessage());
}
}

/**
Expand All @@ -62,7 +83,7 @@ public Photo(@NonNull Context context, @NonNull Uri uri, @Nullable String displa
* @throws NumberFormatException If there was a problem parsing the XML.
*/
public Photo(@NonNull File directory, @NonNull File imageFile) throws IOException, NumberFormatException {
this(new ExtendedExifInterface(imageFile.toString()), imageFile.getAbsolutePath(), imageFile.getName());
this(new ExifInterface(imageFile.toString()), imageFile.getAbsolutePath(), imageFile.getName());
}

/**
Expand All @@ -73,7 +94,7 @@ public Photo(@NonNull File directory, @NonNull File imageFile) throws IOExceptio
* @param displayName a name of the image for display purposes
* @throws IOException if location information is missing
*/
private Photo(@NonNull ExtendedExifInterface exif, @NonNull String ref, @Nullable String displayName) throws IOException {
private Photo(@NonNull ExifInterface exif, @NonNull String ref, @Nullable String displayName) throws IOException {
this.ref = ref;
this.displayName = displayName;

Expand All @@ -87,14 +108,14 @@ private Photo(@NonNull ExtendedExifInterface exif, @NonNull String ref, @Nullabl
float lonf = convertToDegree(lonStr);

String lonRef = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if (lonRef != null && !ExtendedExifInterface.EAST.equals(lonRef)) { // deal with the negative degrees
if (lonRef != null && !ExifInterface.LONGITUDE_EAST.equals(lonRef)) { // deal with the negative degrees
lonf = -lonf;
}

float latf = convertToDegree(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));

String latRef = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
if (latRef != null && !ExtendedExifInterface.NORTH.equals(latRef)) {
if (latRef != null && !ExifInterface.LATITUDE_NORTH.equals(latRef)) {
latf = -latf;
}
if (!(Util.notZero(lonf) && Util.notZero(latf))) {
Expand All @@ -104,11 +125,15 @@ private Photo(@NonNull ExtendedExifInterface exif, @NonNull String ref, @Nullabl
lat = (int) (latf * 1E7d);
lon = (int) (lonf * 1E7d);
Log.d(DEBUG_TAG, "lat: " + lat + " lon: " + lon);

String dir = exif.getAttribute(ExtendedExifInterface.TAG_GPS_IMG_DIRECTION);
String dir = exif.getAttribute(ExifInterface.TAG_GPS_IMG_DIRECTION);
if (dir != null) {
direction = (int) Double.parseDouble(dir);
directionRef = exif.getAttribute(ExtendedExifInterface.TAG_GPS_IMG_DIRECTION_REF);
String[] r = dir.split("/");
if (r.length != 2) {
return;
}
direction = (int) (Double.valueOf(r[0]) / Double.valueOf(r[1]));
Log.d(DEBUG_TAG, ExifInterface.TAG_GPS_IMG_DIRECTION + " " + dir);
directionRef = exif.getAttribute(ExifInterface.TAG_GPS_IMG_DIRECTION_REF);
Log.d(DEBUG_TAG, "dir " + dir + " direction " + direction + " ref " + directionRef);
}
}
Expand Down Expand Up @@ -141,7 +166,7 @@ public Photo(int lat, int lon, int direction, @NonNull String ref, @Nullable Str
this.lat = lat;
this.lon = lon;
this.direction = direction;
this.directionRef = ExtendedExifInterface.MAGNETIC_NORTH;
this.directionRef = ExifInterface.GPS_DIRECTION_MAGNETIC;
this.ref = ref;
this.displayName = displayName;
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/de/blau/android/photos/PhotoIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
public class PhotoIndex extends SQLiteOpenHelper {

private static final int DATA_VERSION = 6;
private static final String DEBUG_TAG = "PhotoIndex";
public static final String DB_NAME = PhotoIndex.class.getSimpleName();
private static final String DEBUG_TAG = DB_NAME;

private static final String NOVESPUCCI = ".novespucci";

Expand Down Expand Up @@ -291,16 +292,15 @@ private void indexDirectories() {
} finally {
close(dbresult2);
}
} else {
Log.d(DEBUG_TAG, "Directory " + indir.getAbsolutePath() + " doesn't exist");
// remove all entries for this directory
db.delete(PHOTOS_TABLE, URI_WHERE, new String[] { indir.getAbsolutePath() });
db.delete(PHOTOS_TABLE, "dir LIKE ?", new String[] { indir.getAbsolutePath() + "/%" });
continue;
}
Log.d(DEBUG_TAG, "Directory " + indir.getAbsolutePath() + " doesn't exist");
// remove all entries for this directory
db.delete(PHOTOS_TABLE, URI_WHERE, new String[] { indir.getAbsolutePath() });
db.delete(PHOTOS_TABLE, "dir LIKE ?", new String[] { indir.getAbsolutePath() + "/%" });
}
dbresult.moveToNext();
}

} catch (SQLiteException ex) {
// Don't crash just report
ACRAHelper.nocrashReport(ex, ex.getMessage());
Expand Down
142 changes: 0 additions & 142 deletions src/main/java/de/blau/android/util/ExtendedExifInterface.java

This file was deleted.

9 changes: 7 additions & 2 deletions src/testCommon/java/de/blau/android/JavaResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import java.io.OutputStream;

import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import de.blau.android.util.FileUtil;

public final class JavaResources {

private static final String DEBUG_TAG = JavaResources.class.getSimpleName();

/**
* Private constructor to stop instantiation
*/
Expand Down Expand Up @@ -49,8 +52,8 @@ public static File copyFileFromResources(@NonNull Context context, @NonNull Stri
* @throws IOException if copying goes wrong
* @throws FileNotFoundException if the file is not found
*/
public static void copyFileFromResources(@NonNull String fileName, @Nullable String source, @NonNull File destinationFile)
throws IOException {
public static void copyFileFromResources(@NonNull String fileName, @Nullable String source, @NonNull File destinationFile) throws IOException {
Log.d(DEBUG_TAG, "args " + fileName + " " + source + " " + destinationFile.getAbsolutePath());
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try (OutputStream os = new FileOutputStream(destinationFile); InputStream is = loader.getResourceAsStream((source != null ? source : "") + fileName)) {
byte[] buffer = new byte[8 * 1024];
Expand All @@ -59,6 +62,8 @@ public static void copyFileFromResources(@NonNull String fileName, @Nullable Str
os.write(buffer, 0, bytesRead);
}
os.flush();
} catch (Exception ex) {
Log.e(DEBUG_TAG, ex.getMessage());
}
}
}

0 comments on commit 9a04331

Please sign in to comment.