Android 图片Exif信息相关的获取与修改

1 Exif是什么

  Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈、快门、平衡白、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码以及GPS等。简单来 说,Exif=拍摄参数+JPED。因此,可以利用任何可以查看JPEG文件的看图软件浏览Exif信息,但是并不是所有图形程序都能处理Exif信息, 而自Android2.0之后,加入了对图片Exif数据的支持。

2 ExifInterface类介绍

  在Android下,通过ExifInterface类操作图片的Exif信息,虽然这个类的名字包含Interface,但它不是一个接口, 它是一个类,处于"android.media.ExifInterface"包下,是媒体库的一部分功能的实现。ExifInterface有一个构造 函数,接受一个String类型的数据,此为读取图片文件的地址。

  Exif数据在图片中可以理解为Key-value键值对的方式存储,一般通过如下几个方法操作:

  String getAttribute(String tag):获取图片中属性为tag的字符串值。

  double getAttribute(String tag,double defaultValue):获取图片中属性为tag的double值。

  int getAttributeInt(String tag,defaultValue):获取图片中属性为tag的int值。

  void setAttribute(String tag,String value):根据输入参数,设定图片Exif的值。

  void saveAttrubutes():把内存中图片的Exif写入到图片中。

3 上代码

public class MainActivity extends Activity {

  ArrayList<String> fileNames = new ArrayList<String>(); //本地图片路径
  String imgPath;
  StringBuilder text = new StringBuilder();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    

    fileNames.clear();
    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) {
      byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); //图片的保存位置的数据
      fileNames.add(new String(data, 0, data.length - 1));
    }
    
    if (fileNames.size() > 0) {
      imgPath = fileNames.get(4);
    }
    
    TextView show = (TextView)findViewById(R.id.show);
    //show.setText(imgPath);
    
    /*
     * 目前Android SDK定义的Tag有:
    
    public static final String TAG_ORIENTATION = "Orientation"; 方向
    public static final String TAG_DATETIME = "DateTime"; 时间日期
    public static final String TAG_MAKE = "Make"; 设备制造商
    public static final String TAG_MODEL = "Model"; 设备型号
    public static final String TAG_FLASH = "Flash"; 闪光灯
    public static final String TAG_IMAGE_WIDTH = "ImageWidth"; 图片宽
    public static final String TAG_IMAGE_LENGTH = "ImageLength"; 图片长
    public static final String TAG_GPS_LATITUDE = "GPSLatitude"; 纬度
    public static final String TAG_GPS_LONGITUDE = "GPSLongitude"; 经度
    public static final String TAG_GPS_LATITUDE_REF = "GPSLatitudeRef"; 纬度参考 
    public static final String TAG_GPS_LONGITUDE_REF = "GPSLongitudeRef"; 经度参考 
    public static final String TAG_EXPOSURE_TIME = "ExposureTime"; 曝光时间
    public static final String TAG_APERTURE = "FNumber"; 光圈值
    public static final String TAG_ISO = "ISOSpeedRatings";
    */ 

    try {
      //android读取图片EXIF信息
      ExifInterface exifInterface=new ExifInterface(imgPath);
      String Orientation=exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
      String DateTime=exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
      String Make=exifInterface.getAttribute(ExifInterface.TAG_MAKE);
      String Model=exifInterface.getAttribute(ExifInterface.TAG_MODEL);
      String Flash=exifInterface.getAttribute(ExifInterface.TAG_FLASH);
      String ImageWidth=exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
      String ImageLength=exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
      String ExposureTime=exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);
      String FNumber=exifInterface.getAttribute(ExifInterface.TAG_APERTURE);
      String ISOSpeedRatings=exifInterface.getAttribute(ExifInterface.TAG_ISO);
      
      text.append("路径---" + imgPath + "\n");
      text.append("方向---" + Orientation + "\n");
      text.append("时间日期---" + DateTime + "\n");
      text.append("设备制造商---" + Make + "\n");
      text.append("设备型号---" + Model + "\n");
      text.append("闪光灯---" + Flash + "\n");
      text.append("图片宽---" + ImageWidth + "\n");
      text.append("图片长---" + ImageLength + "\n");
      text.append("曝光时间---" + ExposureTime + "\n");
      text.append("光圈值---" + FNumber + "\n");
      text.append("ISOSpeedRatings---" + ISOSpeedRatings + "\n");
      
      exifInterface.setAttribute("mytag", "test");
      text.append("mytag---" + exifInterface.getAttribute("mytag") + "\n");
      
    } catch (Exception e) {
      e.printStackTrace();
    }

    show.setText(text);
    
  }

  
}

注意要加上权限
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

code见https://github.com/huanyi0723/ExifDemo

 

转载于:https://www.cnblogs.com/huanyi0723/p/5075741.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值