Android獲取圖片拍照時間

as2020200 8年前發布 | 40K 次閱讀 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<String, String> 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<String, String> 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();
            }
        }
    });
}

Android獲取圖片拍照時間

Log

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


 

來自:文/白一辰(簡書)
 

 本文由用戶 as2020200 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!