温故而知新 知识整理之Bitmap相关类ExifInterface

来源:互联网 发布:淘宝哪家stussy是真的 编辑:程序博客网 时间:2024/04/29 08:40

ExifInterface类主要描述多媒体比如JPG图片的一些附加信息,例如照片的拍摄时间、旋转角度、设备制造商、经纬度等等。

该类有一个构造函数该类有一个构造函数ExifInterface(String filename),提供了多个有用的方法:

public byte[] getThumbnail()   //获取缩略图字节数组public double getAltitude(double defaultValue)   //获取海拔高度public long getDateTime()    //<span style="white-space:pre"></span>获取时间
还可以通过TAG获取对应的属性值:

publicintgetAttributeInt(Stringtag,intdefaultValue)publicStringgetAttribute(Stringtag)
目前主要的一些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 白平衡

不过,在工作中目前只用到获取方向这个TAG,项目中有用到调用系统拍照功能,有些手机返回的Bitmap是横向的,需要自行调整Bitmap方向:

int width = bitmap.getWidth();int height = bitmap.getHeight();int degree = readPictureDegree(filePath);if (degree != 0){Matrix matrix = new Matrix();matrix.setRotate(angle); // 旋转angle度}bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

/** * 读取图片属性:旋转的角度 *  * @param path图片绝对路径 * @return degree旋转的角度 */public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}


0 0
原创粉丝点击