android - 在 Android 中写入/地理标记 JPEG(EXIF 数据)

标签 android exif

我想做的事:
使用我自己的 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 欲望)

编辑:
- 相机设置为 Geotag photos: On
- 我试过硬编码虚拟 GPS 位置

这里是手动设置参数的代码(尝试过先删除 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文件写入SD卡的代码如下:

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/

相关文章:

android - 启动画面 react native 后的白屏

android - 删除系列指示器/绘图图例 AndroidPlot

javascript - JS 客户端 Exif 方向 : Rotate and Mirror JPEG Images

android - ExifInterface 获取照片的位置

android - 来自 ActivityGroup 的子 Activity 仅 "sometimes"调用 onBackPressed()

android - 带电容器的 Ionic 和 Android 不在 IntelliJ 中运行模拟器

android - ViewPager PagerAdapter 在方向改变时重置

Android:旋转位图一次或使用EXIF方向旋转imageview

复制 exif 标签时出现 Android NullPointerException

ios - 在 iOS 中使用照片保存位置 exif 数据