我想做的事:
使用我自己的PictureActivity *拍照并添加EXIF(地理标记)数据
*:实现SurfaceHolder.Callback
并使用Camera
什么不起作用:
添加EXIF GPS数据
我尝试过的
使用ExifInterface
并手动设置Camera.Parameters
(这两种方法均适用于设置GPS元数据的特定方法,也可以使用params.set(String, Value)
)。
我正在使用FlickrJ将图片上传到Flickr(是的,我已经将Flickr设置为导入GPS数据-其他图片工作正常),但是此工具还表示EXIF中没有GPS数据:http://regex.info/exif.cgi
我想念什么?
(Android 2.2,HTC Desire)
编辑:
-相机设置为Geotag photos: On
-我尝试过使用硬编码的虚拟GPS位置
以下是用于手动设置参数的代码(先删除GPS数据后再进行删除,以及先删除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);
}
这是使用
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());
}
这是将JPEG文件写入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);
}
}
};
还尝试从
Bitmap
写入图像(无效),再加上一个问题,在这里报告了使用FileOutputStream
写入的效果 最佳答案
不幸的是,这仅在半球的四分之一上起作用。格林威治以东,赤道以北。我就是这么想的,你必须住在那儿:)。您的“Math.floor”将使所有负值都不正确(例如-105变为-106)。这是同一件事,即使在美国也应如此。
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;
}
...一旦你让我开始,这是相反的
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;
}
……有一天,我将开始编写漂亮的代码。快乐的地理标记,肖恩
关于android - 在Android中写入/地理标记JPEG(EXIF数据),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10531544/