android 图片拍照,Android获取图片拍照时间

为什么写这篇文章是因为今早有个需求需要获取图片拍照时的时间进行一些处理,有些方法参数名忘记了,所以谷歌百度了一下,Android 图片 时间,Android 图片 拍照 时间,这几个关键字居然无法搜索到,那么就在这里留一个印记给需要的人吧。

前言

做过相册的小伙伴应该知道有一个功能是根据照片的拍照时间去排序照片,可以直接从ContentProvider中获取到图片的一些信息,那么直接给你一张图片,你要如何获取图片的信息呢?这就要用到ExifInterface类了。看名字也知道这个类应该分成两段,即"Exif","Interface",也就是说这个类应该是Android对Exif的实现,那么Exif是什么呢?

Exif简介

Exif是一种图像文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及GPS全球定位系统数据、缩略图等。你可以利用任何可以查看JPEG文件的看图软件浏览Exif格式的照片,但并不是所有的图形程序都能处理Exif信息。

ps:以上简介来着百科

ExifInterface简介

ExifInterface是Android下一个操作Exif信息的实现类,是媒体库的功能实现类。

构造函数

/**

* Reads Exif tags from the specified JPEG file.

*/

public ExifInterface(String filename) throws IOException {

if (filename == null) {

throw new IllegalArgumentException("filename cannot be null");

}

mFilename = filename;

loadAttributes();

}

构造函数接收一个字符串形式的图片地址:

/storage/emulated/0/DCIM/P60626-135914.jpg

使用方法

初始化后,系统会把解析好的图片Exif信息使用键值对的方式存储在private HashMap mAttributes;中,这时我们使用getAttribute(String tag)方法就可以获取到相应的值了。

其中主要方法有:

/**

* Returns the value of the specified tag or {@code null} if there

* is no such tag in the JPEG file.

*

* @param tag the name of the tag.

*/

public String getAttribute(String tag) {

return mAttributes.get(tag);

}

public double getAttributeDouble(String tag, double defaultValue) {

...

}

public double getAttributeInt(String tag, double defaultValue) {

...

}

/**

* Set the value of the specified tag.

*

* @param tag the name of the tag.

* @param value the value of the tag.

*/

public void setAttribute(String tag, String value) {

mAttributes.put(tag, value);

}

public void saveAttributes() throws IOException {

...

}

getAttributeDouble()和getAttributeDouble()的实现只是调用getAttribute()后进行一些转换而已。

这里我们主要看一下saveAttributes()方法的源码:

public void saveAttributes() throws IOException {

// format of string passed to native C code:

// "attrCnt attr1=valueLen value1attr2=value2Len value2..."

// example:

// "4 attrPtr ImageLength=4 1024Model=6 FooImageWidth=4 1280Make=3 FOO"

StringBuilder sb = new StringBuilder();

int size = mAttributes.size();

if (mAttributes.containsKey("hasThumbnail")) {

--size;

}

sb.append(size + " ");

for (Map.Entry iter : mAttributes.entrySet()) {

String key = iter.getKey();

if (key.equals("hasThumbnail")) {

// this is a fake attribute not saved as an exif tag

continue;

}

String val = iter.getValue();

sb.append(key + "=");

sb.append(val.length() + " ");

sb.append(val);

}

String s = sb.toString();

synchronized (sLock) {

saveAttributesNative(mFilename, s);

commitChangesNative(mFilename);

}

}

可以看出,先是entrySet遍历了所以的属性值,确实是使用的键值对的方式进行存储Exif信息的。

常用的Exif支持的tag有:

TAG_APERTURE:光圈值

TAG_DATETIME:拍摄时间(取决于设备设置的时间)

TAG_EXPOSURE_TIME:曝光时间

TAG_FLASH:闪光灯

TAG_FOCAL_LENGTH:焦距

TAG_IMAGE_LENGTH:图片高度

TAG_IMAGE_WIDTH:图片宽度

TAG_ISO:ISO

TAG_MAKE:设备

TAG_MODEL:设备型号

TAG_ORIENTATION:旋转角度

ps:全部tag查看ExifInterface的源码吧,还挺多的。

简单的Demo演示

mImageView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

try {

String path = (String) v.getTag();

Log.i(TAG, "path:" + path);

ExifInterface exifInterface = new ExifInterface(path);

String TAG_APERTURE = exifInterface.getAttribute(ExifInterface.TAG_APERTURE);

String TAG_DATETIME = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);

String TAG_EXPOSURE_TIME = exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);

String TAG_FLASH = exifInterface.getAttribute(ExifInterface.TAG_FLASH);

String TAG_FOCAL_LENGTH = exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);

String TAG_IMAGE_LENGTH = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);

String TAG_IMAGE_WIDTH = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);

String TAG_ISO = exifInterface.getAttribute(ExifInterface.TAG_ISO);

String TAG_MAKE = exifInterface.getAttribute(ExifInterface.TAG_MAKE);

String TAG_MODEL = exifInterface.getAttribute(ExifInterface.TAG_MODEL);

String TAG_ORIENTATION = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);

String TAG_WHITE_BALANCE = exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);

Log.i(TAG, "光圈值:" + TAG_APERTURE);

Log.i(TAG, "拍摄时间:" + TAG_DATETIME);

Log.i(TAG, "曝光时间:" + TAG_EXPOSURE_TIME);

Log.i(TAG, "闪光灯:" + TAG_FLASH);

Log.i(TAG, "焦距:" + TAG_FOCAL_LENGTH);

Log.i(TAG, "图片高度:" + TAG_IMAGE_LENGTH);

Log.i(TAG, "图片宽度:" + TAG_IMAGE_WIDTH);

Log.i(TAG, "ISO:" + TAG_ISO);

Log.i(TAG, "设备品牌:" + TAG_MAKE);

Log.i(TAG, "设备型号:" + TAG_MODEL);

Log.i(TAG, "旋转角度:" + TAG_ORIENTATION);

Log.i(TAG, "白平衡:" + TAG_WHITE_BALANCE);

/*

Date date = UtilsTime.stringTimeToDate(TAG_DATETIME, new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.getDefault()));

String FStringTime = UtilsTime.dateToStringTime(date, new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault()));

mTextView.setText("TAG_DATETIME = " + TAG_DATETIME + "\n" + "FStringTime = " + FStringTime);

*/

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

1f2219c92b0e6469a00029c93c05f05e.png

Log

Whitelaning

It's very easy to be different but very difficult to be better

来自:文/白一辰(简书)

(责任编辑:ioter)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值