Android开发之使用ExifInterface获取拍照后的图片属性

本文实例讲述了Android开发之使用ExifInterface获取拍照后的图片属性。分享给大家供大家参考,具体如下:

ExifInterface exif = new ExifInterface(file.getPath());
String widthStr = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
String heightStr = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
......
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.media;
import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
/**
 * This is a class for reading and writing Exif tags in a JPEG file.
 */
public class ExifInterface {
  // The Exif tag names
  /** Type is int. */
  public static final String TAG_ORIENTATION = "Orientation";
  /** Type is String. */
  public static final String TAG_DATETIME = "DateTime";
  /** Type is String. */
  public static final String TAG_MAKE = "Make";
  /** Type is String. */
  public static final String TAG_MODEL = "Model";
  /** Type is int. */
  public static final String TAG_FLASH = "Flash";
  /** Type is int. */
  public static final String TAG_IMAGE_WIDTH = "ImageWidth";
  /** Type is int. */
  public static final String TAG_IMAGE_LENGTH = "ImageLength";
  /** String. Format is "num1/denom1,num2/denom2,num3/denom3". */
  public static final String TAG_GPS_LATITUDE = "GPSLatitude";
  /** String. Format is "num1/denom1,num2/denom2,num3/denom3". */
  public static final String TAG_GPS_LONGITUDE = "GPSLongitude";
  /** Type is String. */
  public static final String TAG_GPS_LATITUDE_REF = "GPSLatitudeRef";
  /** Type is String. */
  public static final String TAG_GPS_LONGITUDE_REF = "GPSLongitudeRef";
  /** Type is String. */
  public static final String TAG_GPS_TIMESTAMP = "GPSTimeStamp";
  /** Type is String. */
  public static final String TAG_GPS_DATESTAMP = "GPSDateStamp";
  /** Type is int. */
  public static final String TAG_WHITE_BALANCE = "WhiteBalance";
  /** Type is rational. */
  public static final String TAG_FOCAL_LENGTH = "FocalLength";
  /** Type is String. Name of GPS processing method used for location finding. */
  public static final String TAG_GPS_PROCESSING_METHOD = "GPSProcessingMethod";
  // Constants used for the Orientation Exif tag.
  public static final int ORIENTATION_UNDEFINED = 0;
  public static final int ORIENTATION_NORMAL = 1;
  public static final int ORIENTATION_FLIP_HORIZONTAL = 2; // left right reversed mirror
  public static final int ORIENTATION_ROTATE_180 = 3;
  public static final int ORIENTATION_FLIP_VERTICAL = 4; // upside down mirror
  public static final int ORIENTATION_TRANSPOSE = 5; // flipped about top-left <--> bottom-right axis
  public static final int ORIENTATION_ROTATE_90 = 6; // rotate 90 cw to right it
  public static final int ORIENTATION_TRANSVERSE = 7; // flipped about top-right <--> bottom-left axis
  public static final int ORIENTATION_ROTATE_270 = 8; // rotate 270 to right it
  // Constants used for white balance
  public static final int WHITEBALANCE_AUTO = 0;
  public static final int WHITEBALANCE_MANUAL = 1;
  private static SimpleDateFormat sFormatter;
  static {
    System.loadLibrary("exif");
    sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    sFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
  }
  private String mFilename;
  private HashMap<String, String> mAttributes;
  private boolean mHasThumbnail;
  // Because the underlying implementation (jhead) uses static variables,
  // there can only be one user at a time for the native functions (and
  // they cannot keep state in the native code across function calls). We
  // use sLock to serialize the accesses.
  private static Object sLock = new Object();
  /**
   * Reads Exif tags from the specified JPEG file.
   */
  public ExifInterface(String filename) throws IOException {
    mFilename = filename;
    loadAttributes();
  }
  /**
   * 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);
  }
  /**
   * Returns the integer value of the specified tag. If there is no such tag
   * in the JPEG file or the value cannot be parsed as integer, return
   * <var>defaultValue</var>.
   *
   * @param tag the name of the tag.
   * @param defaultValue the value to return if the tag is not available.
   */
  public int getAttributeInt(String tag, int defaultValue) {
    String value = mAttributes.get(tag);
    if (value == null) return defaultValue;
    try {
      return Integer.valueOf(value);
    } catch (NumberFormatException ex) {
      return defaultValue;
    }
  }
  /**
   * Returns the double value of the specified rational tag. If there is no
   * such tag in the JPEG file or the value cannot be parsed as double, return
   * <var>defaultValue</var>.
   *
   * @param tag the name of the tag.
   * @param defaultValue the value to return if the tag is not available.
   */
  public double getAttributeDouble(String tag, double defaultValue) {
    String value = mAttributes.get(tag);
    if (value == null) return defaultValue;
    try {
      int index = value.indexOf("/");
      if (index == -1) return defaultValue;
      double denom = Double.parseDouble(value.substring(index + 1));
      if (denom == 0) return defaultValue;
      double num = Double.parseDouble(value.substring(0, index));
      return num / denom;
    } catch (NumberFormatException ex) {
      return 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);
  }
  /**
   * Initialize mAttributes with the attributes from the file mFilename.
   *
   * mAttributes is a HashMap which stores the Exif attributes of the file.
   * The key is the standard tag name and the value is the tag's value: e.g.
   * Model -> Nikon. Numeric values are stored as strings.
   *
   * This function also initialize mHasThumbnail to indicate whether the
   * file has a thumbnail inside.
   */
  private void loadAttributes() throws IOException {
    // format of string passed from native C code:
    // "attrCnt attr1=valueLen value1attr2=value2Len value2..."
    // example:
    // "4 attrPtr ImageLength=4 1024Model=6 FooImageWidth=4 1280Make=3 FOO"
    mAttributes = new HashMap<String, String>();
    String attrStr;
    synchronized (sLock) {
      attrStr = getAttributesNative(mFilename);
    }
    // get count
    int ptr = attrStr.indexOf(' ');
    int count = Integer.parseInt(attrStr.substring(0, ptr));
    // skip past the space between item count and the rest of the attributes
    ++ptr;
    for (int i = 0; i < count; i++) {
      // extract the attribute name
      int equalPos = attrStr.indexOf('=', ptr);
      String attrName = attrStr.substring(ptr, equalPos);
      ptr = equalPos + 1;   // skip past =
      // extract the attribute value length
      int lenPos = attrStr.indexOf(' ', ptr);
      int attrLen = Integer.parseInt(attrStr.substring(ptr, lenPos));
      ptr = lenPos + 1;    // skip pas the space
      // extract the attribute value
      String attrValue = attrStr.substring(ptr, ptr + attrLen);
      ptr += attrLen;
      if (attrName.equals("hasThumbnail")) {
        mHasThumbnail = attrValue.equalsIgnoreCase("true");
      } else {
        mAttributes.put(attrName, attrValue);
      }
    }
  }
  /**
   * Save the tag data into the JPEG file. This is expensive because it involves
   * copying all the JPG data from one file to another and deleting the old file
   * and renaming the other. It's best to use {@link #setAttribute(String,String)}
   * to set all attributes to write and make a single call rather than multiple
   * calls for each attribute.
   */
  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);
    }
  }
  /**
   * Returns true if the JPEG file has a thumbnail.
   */
  public boolean hasThumbnail() {
    return mHasThumbnail;
  }
  /**
   * Returns the thumbnail inside the JPEG file, or {@code null} if there is no thumbnail.
   * The returned data is in JPEG format and can be decoded using
   * {@link android.graphics.BitmapFactory#decodeByteArray(byte[],int,int)}
   */
  public byte[] getThumbnail() {
    synchronized (sLock) {
      return getThumbnailNative(mFilename);
    }
  }
  /**
   * Stores the latitude and longitude value in a float array. The first element is
   * the latitude, and the second element is the longitude. Returns false if the
   * Exif tags are not available.
   */
  public boolean getLatLong(float output[]) {
    String latValue = mAttributes.get(ExifInterface.TAG_GPS_LATITUDE);
    String latRef = mAttributes.get(ExifInterface.TAG_GPS_LATITUDE_REF);
    String lngValue = mAttributes.get(ExifInterface.TAG_GPS_LONGITUDE);
    String lngRef = mAttributes.get(ExifInterface.TAG_GPS_LONGITUDE_REF);
    if (latValue != null && latRef != null && lngValue != null && lngRef != null) {
      output[0] = convertRationalLatLonToFloat(latValue, latRef);
      output[1] = convertRationalLatLonToFloat(lngValue, lngRef);
      return true;
    } else {
      return false;
    }
  }
  /**
   * Returns number of milliseconds since Jan. 1, 1970, midnight.
   * Returns -1 if the date time information if not available.
   * @hide
   */
  public long getDateTime() {
    String dateTimeString = mAttributes.get(TAG_DATETIME);
    if (dateTimeString == null) return -1;
    ParsePosition pos = new ParsePosition(0);
    try {
      Date datetime = sFormatter.parse(dateTimeString, pos);
      if (datetime == null) return -1;
      return datetime.getTime();
    } catch (IllegalArgumentException ex) {
      return -1;
    }
  }
  /**
   * Returns number of milliseconds since Jan. 1, 1970, midnight UTC.
   * Returns -1 if the date time information if not available.
   * @hide
   */
  public long getGpsDateTime() {
    String date = mAttributes.get(TAG_GPS_DATESTAMP);
    String time = mAttributes.get(TAG_GPS_TIMESTAMP);
    if (date == null || time == null) return -1;
    String dateTimeString = date + ' ' + time;
    if (dateTimeString == null) return -1;
    ParsePosition pos = new ParsePosition(0);
    try {
      Date datetime = sFormatter.parse(dateTimeString, pos);
      if (datetime == null) return -1;
      return datetime.getTime();
    } catch (IllegalArgumentException ex) {
      return -1;
    }
  }
  private static float convertRationalLatLonToFloat(
      String rationalString, String ref) {
    try {
      String [] parts = rationalString.split(",");
      String [] pair;
      pair = parts[0].split("/");
      int degrees = (int) (Float.parseFloat(pair[0].trim())
          / Float.parseFloat(pair[1].trim()));
      pair = parts[1].split("/");
      int minutes = (int) ((Float.parseFloat(pair[0].trim())
          / Float.parseFloat(pair[1].trim())));
      pair = parts[2].split("/");
      float seconds = Float.parseFloat(pair[0].trim())
          / Float.parseFloat(pair[1].trim());
      float result = degrees + (minutes / 60F) + (seconds / (60F * 60F));
      if ((ref.equals("S") || ref.equals("W"))) {
        return -result;
      }
      return result;
    } catch (RuntimeException ex) {
      // if for whatever reason we can't parse the lat long then return
      // null
      return 0f;
    }
  }
  private native boolean appendThumbnailNative(String fileName,
      String thumbnailFileName);
  private native void saveAttributesNative(String fileName,
      String compressedAttributes);
  private native String getAttributesNative(String fileName);
  private native void commitChangesNative(String fileName);
  private native byte[] getThumbnailNative(String fileName);
}

更多关于Android开发相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》及《Android图形与图像处理技巧总结》

希望本文所述对大家Android程序设计有所帮助。

(0)

相关推荐

  • Android实现从本地图库/相机拍照后裁剪图片并设置头像

    玩qq或者是微信的盆友都知道,这些聊天工具里都要设置头像,一般情况下大家的解决办法是从本地图库选择图片或是从相机拍照,然后根据自己的喜爱截取图片.上述过程已经实现好了,最后一步我加上了把截取好的图片在保存到本地的操作,来保存头像.为了大家需要,下面我们小编把完整的代码贴出来供大家参考. 先给大家展示效果图: 代码部分: 布局代码(其实就是两个按钮和一个ImageView来显示头像) <LinearLayout xmlns:android="http://schemas.android.co

  • Android拍照和获取相册图片

    之前遇到各种拍照啊,获取相册图片之类,都是直接去度娘,要么之前的代码复制下,没好好总结过. 再也不要问度娘了,再也不用一堆博客里找啊找了... ----------------------------------------------我是正文的分割线----------------------------------------------------------- 一个一个来,先说调用手机相机拍照(最简单版): cameraButton.setOnClickListener(new View

  • Android实现拍照及图片显示效果

    本文实例为大家分享了Android拍照及图片显示的具体代码,供大家参考,具体内容如下 1.功能声明 当应用需要使用相机.NFC等外设时,需要在AndroidManifest.xml中进行声明. 这样,当设备缺少这些外设时,应用商店的安装程序可以拒绝安装设备. 声明示例代码如下: <uses-feature android:name="android.hardware.camera2" <!-- required为false时,不强制要求设备支持该功能 --> <

  • Android拍照得到全尺寸图片并进行压缩

    废话不多说了,直接给大家贴代码了,具体代码如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <

  • Android实现拍照、选择图片并裁剪图片功能

    一. 实现拍照.选择图片并裁剪图片效果 按照之前博客的风格,首先看下实现效果. 二. uCrop项目应用 想起之前看到的Yalantis/uCrop效果比较绚,但是研究源码之后发现在定制界面方面还是有一点的限制,于是在它的基础上做了修改Android-Crop,把定制界面独立出来,让用户去自由设置.下图为使用Android-Crop实现的模仿微信选择图片并裁剪Demo. 三. 实现思路 比较简单的选择设备图片裁剪,并将裁剪后的图片保存到指定路径: 调用系统拍照,将拍照图片保存在SD卡,然后裁剪图

  • Android拍照裁剪图片

    下面是效果图,看看是不是亲想要的效果图,如果是,这段代码你就可以参考下了,但是要灵活运用,根据需求做相应的改动. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation

  • Android启动相机拍照并返回图片

    具体实现过程请看下面代码: 简单的调用了一下系统的拍照功能 代码如下所示: //拍照的方法 private void openTakePhoto(){ /** * 在启动拍照之前最好先判断一下sdcard是否可用 */ String state = Environment.getExternalStorageState(); //拿到sdcard是否可用的状态码 if (state.equals(Environment.MEDIA_MOUNTED)){ //如果可用 Intent intent

  • android完美实现 拍照 选择图片 剪裁等代码分享

    前言,版本兼容问题主要是由于4.4以前和4.4以后的Uri的格式不同所造成的错误 1.拍照 和选择图片   ①选择图片 intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, GALLERY_REQUEST_CODE); ②拍照 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE)

  • Android手机拍照或选取图库图片作为头像

    package zhangpgil.photo; import java.io.File; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import android.content.Intent; import

  • android图像绘制(六)获取本地图片或拍照图片等图片资源

    从SD卡中获取图片资源,或者拍一张新的图片. 先贴代码 获取图片: 注释:拍照获取的话,可以指定图片的保存地址,在此不说明. 复制代码 代码如下: CharSequence[] items = {"相册", "相机"}; new AlertDialog.Builder(this) .setTitle("选择图片来源") .setItems(items, new OnClickListener() { public void onClick(Dia

随机推荐