Skip to content

Commit 43a7eed

Browse files
authored
Merge pull request ypresto#63 from hkurokawa/support-geo-metatag
Support geo metadata
2 parents e9c411c + bfe2807 commit 43a7eed

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.ypresto.androidtranscoder.utils;
2+
3+
import junit.framework.TestCase;
4+
5+
public class ISO6709LocationParserTest extends TestCase {
6+
public void testParse() {
7+
ISO6709LocationParser parser = new ISO6709LocationParser();
8+
assertEquals(new float[]{35.658632f, 139.745411f}, parser.parse("+35.658632+139.745411/"));
9+
assertEquals(new float[]{40.75f, -074.00f}, parser.parse("+40.75-074.00/"));
10+
// with Altitude
11+
assertEquals(new float[]{-90f, +0f}, parser.parse("-90+000+2800/"));
12+
assertEquals(new float[]{27.5916f, 086.5640f}, parser.parse("+27.5916+086.5640+8850/"));
13+
// ranged data
14+
assertEquals(new float[]{35.331f, 134.224f}, parser.parse("+35.331+134.224/+35.336+134.228/"));
15+
assertEquals(new float[]{35.331f, 134.224f}, parser.parse("+35.331+134.224/+35.336+134.228/+35.333+134.229/+35.333+134.227/"));
16+
}
17+
18+
public void testParseFailure() {
19+
ISO6709LocationParser parser = new ISO6709LocationParser();
20+
assertNull(parser.parse(null));
21+
assertNull(parser.parse(""));
22+
assertNull(parser.parse("35 deg 65' 86.32\" N, 139 deg 74' 54.11\" E"));
23+
assertNull(parser.parse("+35.658632"));
24+
assertNull(parser.parse("+35.658632-"));
25+
assertNull(parser.parse("40.75-074.00"));
26+
assertNull(parser.parse("+40.75-074.00.00"));
27+
}
28+
29+
private static void assertEquals(float[] expected, float[] actual) {
30+
assertEquals(expected.length, actual.length);
31+
for (int i = 0; i < expected.length; i++) {
32+
assertTrue(Float.compare(expected[i], actual[i]) == 0);
33+
}
34+
}
35+
}

lib/src/main/java/net/ypresto/androidtranscoder/engine/MediaTranscoderEngine.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import android.media.MediaFormat;
2020
import android.media.MediaMetadataRetriever;
2121
import android.media.MediaMuxer;
22+
import android.os.Build;
2223
import android.util.Log;
2324

25+
import net.ypresto.androidtranscoder.BuildConfig;
2426
import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
27+
import net.ypresto.androidtranscoder.utils.ISO6709LocationParser;
2528
import net.ypresto.androidtranscoder.utils.MediaExtractorUtils;
2629

2730
import java.io.FileDescriptor;
@@ -137,9 +140,17 @@ private void setupMetadata() throws IOException {
137140
// skip
138141
}
139142

140-
// TODO: parse ISO 6709
141-
// String locationString = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
142-
// mMuxer.setLocation(Integer.getInteger(rotationString, 0));
143+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
144+
String locationString = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
145+
if (locationString != null) {
146+
float[] location = new ISO6709LocationParser().parse(locationString);
147+
if (location != null) {
148+
mMuxer.setLocation(location[0], location[1]);
149+
} else {
150+
Log.d(TAG, "Failed to parse the location metadata: " + locationString);
151+
}
152+
}
153+
}
143154

144155
try {
145156
mDurationUs = Long.parseLong(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.ypresto.androidtranscoder.utils;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class ISO6709LocationParser {
7+
private final Pattern pattern;
8+
9+
public ISO6709LocationParser() {
10+
this.pattern = Pattern.compile("([+\\-][0-9.]+)([+\\-][0-9.]+)");
11+
}
12+
13+
/**
14+
* This method parses the given string representing a geographic point location by coordinates in ISO 6709 format
15+
* and returns the latitude and the longitude in float. If <code>location</code> is not in ISO 6709 format,
16+
* this method returns <code>null</code>
17+
*
18+
* @param location a String representing a geographic point location by coordinates in ISO 6709 format
19+
* @return <code>null</code> if the given string is not as expected, an array of floats with size 2,
20+
* where the first element represents latitude and the second represents longitude, otherwise.
21+
*/
22+
public float[] parse(String location) {
23+
if (location == null) return null;
24+
Matcher m = pattern.matcher(location);
25+
if (m.find() && m.groupCount() == 2) {
26+
String latstr = m.group(1);
27+
String lonstr = m.group(2);
28+
try {
29+
float lat = Float.parseFloat(latstr);
30+
float lon = Float.parseFloat(lonstr);
31+
return new float[]{lat, lon};
32+
} catch (NumberFormatException ignored) {
33+
}
34+
}
35+
return null;
36+
}
37+
}

0 commit comments

Comments
 (0)