Reputation: 1225
What I want to do:
Take a picture using my own PictureActivity* and add EXIF (geotags) data
*: Implementing SurfaceHolder.Callback
and using Camera
What is not working:
Adding the EXIF GPS data
What I've tried:
Using the ExifInterface
and manually setting Camera.Parameters
(both with the specific methods for setting GPS meta-data and by using params.set(String, Value)
).
I'm uploading the pictures to Flickr using FlickrJ (yes, I've set Flickr to import GPS data -- other pictures work fine), however this tool also says there is no GPS data in the EXIF: http://regex.info/exif.cgi
What am I missing?
(Android 2.2, HTC Desire)
Edit:
- The camera is set to Geotag photos: On
- I've tried with hardcoded dummy GPS positions
Here is the code for manually setting parameters (tried both with and without first removing the GPS data, and as mentioned also with set(String, Value)
):
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(p.getPreviewSize().width, p.getPreviewSize().height);
Log.e("PictureActivity", "EXIF: "+AGlanceLocationListener.getLatitude());
p.removeGpsData();
p.setGpsLatitude( AGlanceLocationListener.getLatitude() );
p.setGpsLongitude( AGlanceLocationListener.getLongitude() );
p.setGpsAltitude( AGlanceLocationListener.getAltitude() );
p.setGpsTimestamp( AGlanceLocationListener.getTime() );
mCamera.setParameters(p);
}
Here is the code for using the ExifInterface
:
//Save EXIF location data to JPEG
ExifInterface exif;
try {
exif = new ExifInterface("/sdcard/DCIM/"+filename+".jpeg");
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
String.valueOf(AGlanceLocationListener.getLatitude()));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
String.valueOf(AGlanceLocationListener.getLongitude()));
exif.saveAttributes();
} catch (IOException e) {
Log.e("PictureActivity", e.getLocalizedMessage());
}
Here is the code for writing the JPEG file to the SDCARD:
Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c)
{
// Bitmap pic = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
String day = String.valueOf(Calendar.getInstance().getTime().getDay());
String hour = String.valueOf(Calendar.getInstance().getTime().getHours());
String minute = String.valueOf(Calendar.getInstance().getTime().getMinutes());
String second = String.valueOf(Calendar.getInstance().getTime().getSeconds());
filename = "Billede"+day+hour+minute+second;
try {
FileOutputStream fos = new FileOutputStream(new File("/sdcard/DCIM/"+filename+".jpeg"));
fos.write(imageData);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
if(imageData != null){
Intent mIntent = new Intent();
setResult(0,mIntent);
PictureActivity.this.showDialog(0);
}
}
};
Also tried writing the image from a Bitmap
(didn't work), plus another question here report writing using a FileOutputStream
worked
Upvotes: 12
Views: 27706
Reputation: 1482
This solution will cater for negative and positive lat/lng values:
static public boolean setGeoTag(File image, LatLng geoTag) {
if (geoTag != null) {
try {
ExifInterface exif = new ExifInterface(
image.getAbsolutePath());
double latitude = Math.abs(geoTag.latitude);
double longitude = Math.abs(geoTag.longitude);
int num1Lat = (int) Math.floor(latitude);
int num2Lat = (int) Math.floor((latitude - num1Lat) * 60);
double num3Lat = (latitude - ((double) num1Lat + ((double) num2Lat / 60))) * 3600000;
int num1Lon = (int) Math.floor(longitude);
int num2Lon = (int) Math.floor((longitude - num1Lon) * 60);
double num3Lon = (longitude - ((double) num1Lon + ((double) num2Lon / 60))) * 3600000;
String lat = num1Lat + "/1," + num2Lat + "/1," + num3Lat + "/1000";
String lon = num1Lon + "/1," + num2Lon + "/1," + num3Lon + "/1000";
if (geoTag.latitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
} else {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
}
if (geoTag.longitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
} else {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
}
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, lat);
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, lon);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
return true;
}
Upvotes: 4
Reputation: 6755
Unfortunately, this works on a quarter of the hemisphere only. East of Greenwich and North of the equator. That's how I guessed you must live there:). Your 'Math.floor' will make all negative values wrong (like -105 into -106). Here's the same thing, that should work even in the US.
public void loc2Exif(String flNm, Location loc) {
try {
ExifInterface ef = new ExifInterface(flNm);
ef.setAttribute(ExifInterface.TAG_GPS_LATITUDE, dec2DMS(loc.getLatitude()));
ef.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,dec2DMS(loc.getLongitude()));
if (loc.getLatitude() > 0)
ef.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
else
ef.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
if (loc.getLongitude()>0)
ef.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
else
ef.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
ef.saveAttributes();
} catch (IOException e) {}
}
//-----------------------------------------------------------------------------------
String dec2DMS(double coord) {
coord = coord > 0 ? coord : -coord; // -105.9876543 -> 105.9876543
String sOut = Integer.toString((int)coord) + "/1,"; // 105/1,
coord = (coord % 1) * 60; // .987654321 * 60 = 59.259258
sOut = sOut + Integer.toString((int)coord) + "/1,"; // 105/1,59/1,
coord = (coord % 1) * 60000; // .259258 * 60000 = 15555
sOut = sOut + Integer.toString((int)coord) + "/1000"; // 105/1,59/1,15555/1000
return sOut;
}
... and once you got me started, here's the reverse
public Location exif2Loc(String flNm) {
String sLat = "", sLatR = "", sLon = "", sLonR = "";
try {
ExifInterface ef = new ExifInterface(flNm);
sLat = ef.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
sLon = ef.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
sLatR = ef.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
sLonR = ef.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
} catch (IOException e) {return null;}
double lat = dms2Dbl(sLat);
if (lat > 180.0) return null;
double lon = dms2Dbl(sLon);
if (lon > 180.0) return null;
lat = sLatR.contains("S") ? -lat : lat;
lon = sLonR.contains("W") ? -lon : lon;
Location loc = new Location("exif");
loc.setLatitude(lat);
loc.setLongitude(lon);
return loc;
}
//-------------------------------------------------------------------------
double dms2Dbl(String sDMS){
double dRV = 999.0;
try {
String[] DMSs = sDMS.split(",", 3);
String s[] = DMSs[0].split("/", 2);
dRV = (new Double(s[0])/new Double(s[1]));
s = DMSs[1].split("/", 2);
dRV += ((new Double(s[0])/new Double(s[1]))/60);
s = DMSs[2].split("/", 2);
dRV += ((new Double(s[0])/new Double(s[1]))/3600);
} catch (Exception e) {}
return dRV;
}
... and one day, I'll start to write pretty looking code. Happy geotagging, sean
Upvotes: 15
Reputation: 1225
Found the problem:
Looking at the original images from the SDCARD showed:
The images contained EXIF GPS data if the EXIFInterface is used. The GPS data, however, was wrong (see below) -- which probably is why Flickr won't show it.
Using the method that sets GPS coordinates through the camera parameters do NOT write EXIF GPS data (this was using dummy hardcoded coordinates, I haven't tested with an actual GPS fix). I haven't looked more into why this is.
The Android API for EXIFInterface has this documentation:
public static final String TAG_GPS_LONGITUDE
Since: API Level 5
String. Format is "num1/denom1,num2/denom2,num3/denom3".
Constant Value: "GPSLongitude"
The problem with my original code is that I was passing the GPS coordinates in Decimal Degrees -- the coordinates you get from calling getLatitude/getLogitude on a Location object is in Decimal Degrees. The EXIFInterface expects the coordinates in Degrees Minutes Seconds and then written as rationals (this is part of the EXIF specification). More on GPS coordinate formats and conversion here.
Here is another question/answer that explains how to convert from Decimal Degrees to Degrees Minutes Seconds.
Using this code, the GPS coordinates gets written correctly in the EXIF and Flickr have no problem importing the data:
ExifInterface exif;
double latitude = AGlanceLocationListener.getLatitude();
double longitude = AGlanceLocationListener.getLongitude();
try {
exif = new ExifInterface("/sdcard/DCIM/"+filename+".jpeg");
int num1Lat = (int)Math.floor(latitude);
int num2Lat = (int)Math.floor((latitude - num1Lat) * 60);
double num3Lat = (latitude - ((double)num1Lat+((double)num2Lat/60))) * 3600000;
int num1Lon = (int)Math.floor(longitude);
int num2Lon = (int)Math.floor((longitude - num1Lon) * 60);
double num3Lon = (longitude - ((double)num1Lon+((double)num2Lon/60))) * 3600000;
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");
if (latitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
} else {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
}
if (longitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
} else {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
}
exif.saveAttributes();
} catch (IOException e) {
Log.e("PictureActivity", e.getLocalizedMessage());
}
Note: When using Degrees Minutes Seconds you also need to set the GPS reference attributes (N, S, E, W).
Upvotes: 13