Android多媒体- ExifInterface读取图片相关信息

来源:互联网 发布:淘宝店怎么快速升钻 编辑:程序博客网 时间:2024/05/16 00:41

从Android 2.0开始新增了ExifInterface类,ExifInterface类主要描述多媒体文件比如JPG格式图片的一些附加信息,比如图片文件的旋转,gps,缩略图等。该类位于android.media.ExifInterface的位置,需要调用API Level至少为5即2.0 SDK。

图片的的Exif信息和MP3的ID3标签类似,使用了属性和值的存储方式。通过public void setAttribute (String tag, String value) 来设置,而获取可以通过 public int getAttributeInt (String tag, int defaultValue) 和 public String getAttribute (String tag) 两种方法都可以,getAttributeInt 重载方法一第二个参数为我们设置的默认值,如果成功则返回相应Tag的值;特定的整数内容为该方法直接返回值。而重载方法二该方法直接返回结果,如果失败则为null。


  目前Android SDK定义的Tag有:


  TAG_DATETIME 时间日期


  TAG_FLASH 闪光灯


  TAG_GPS_LATITUDE 纬度


  TAG_GPS_LATITUDE_REF 纬度参考


  TAG_GPS_LONGITUDE 经度


  TAG_GPS_LONGITUDE_REF 经度参考


  TAG_IMAGE_LENGTH 图片长


  TAG_IMAGE_WIDTH 图片宽


  TAG_MAKE 设备制造商


  TAG_MODEL 设备型号


  TAG_ORIENTATION 方向


  TAG_WHITE_BALANCE 白平衡


举例代码:

Bitmap bitmap =null;      try {     ExifInterface exifInterface = new ExifInterface(file.getPath());     int result = exifInterface.getAttributeInt(             ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);     int rotate = 0;     switch(result) {     case ExifInterface.ORIENTATION_ROTATE_90:         rotate = 90;         break;     case ExifInterface.ORIENTATION_ROTATE_180:         rotate = 180;         break;     case ExifInterface.ORIENTATION_ROTATE_270:         rotate = 270;         break;     default:         break;     }     BitmapFactory.Options options = new BitmapFactory.Options();    options.inPreferredConfig = Bitmap.Config.RGB_565;    options.inJustDecodeBounds = true;    BitmapFactory.decodeFile(filePath, options);    if (options.outWidth < 0 || options.outHeight < 0) {        return null;    }       options.inJustDecodeBounds = false;    bitmap=  BitmapFactory.decodeFile(filePath, options);    if(rotate > 0) {         Matrix matrix = new Matrix();         matrix.setRotate(rotate);         Bitmap rotateBitmap = Bitmap.createBitmap(                 bitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);         if(rotateBitmap != null) {             bitmap.recycle();             bitmap = rotateBitmap;         }     } } catch (IOException e) {     e.printStackTrace(); } 
在指定的tag后,返回一个int类型的值,这里传入的是ExifInterface.TAG_ORIENTATION,所以会返回一个角度的int类型的值,当我们用android 平板电脑,或者手机横屏拍照片时并希望把它作为背景设置在所在的应用背景,而且不希望背景会产生旋转90度得现象


Android 中保存图片的代码(下面链接文章也使用到了ExifInterface,方式还不太一样)

http://blog.csdn.net/wstarx/article/details/6176902




0 0