在Android的源代码里提供了一个专门读写图片信息的类ExifInterface,官方给出的注释为:This is a class for reading and writing Exif tags in a JPEG file ,可见ExifInterface是专门用来读写JPEG图片文件Exif信息的类。
Exif信息里面就包括了角度,GPS经纬度,白平衡,闪光灯等信息。ExifInterface的用法相对简单,只有一个带参的构造方法,将图片文件地址传过去就可以了。类里提供了getAttribute方法来取得各种属性,当得也可以用setAttribute方法来为已存在的图片设置或修改其本来属性。
下面贴上代码:
- /**
- * 读取图片属性:旋转的角度
- * @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;
- }
- return degree;
- }
能过以上方法得到图片角度后,就可以通过Matrix类对图片进行纠正了,还是贴上完整的代码,如下:
- /**
- * 旋转图片,使图片保持正确的方向。
- * @param bitmap 原始图片
- * @param degrees 原始图片的角度
- * @return Bitmap 旋转后的图片
- */
- public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
- if (degrees == 0 || null == bitmap) {
- return bitmap;
- }
- Matrix matrix = new Matrix();
- matrix.setRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
- Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
- if (null != bitmap) {
- bitmap.recycle();
- }
- return bmp;
- }
通过以上两个步骤,就可以得到一个正着的图片了。当然中间省略了一步:
- Bitmap bmp =BitmapFactory.decodeFile(imageFilePath); ————————————————————————————————————————
在android多媒体开发中,ExifInterface(exif exchangeable image file) ,这个接口提供了图片文件的旋转,gps,时间等信息。
- Bitmap bitmap =null;
- int scallType = 0;
- 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;
- }
- scallType = genScallType(context, options);
- 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();
- }
public int getAttributeInt(String tag, int defaultValue)
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, returndefaultValue.
(在指定的tag后,返回一个int类型的值,这里传入的是ExifInterface.TAG_ORIENTATION,所以会返回一个角度的int类型的值,当我们用android 平板电脑,或者手机横屏拍照片时并希望把它作为背景设置在所在的应用背景,而且不希望背景会产生旋转90度得现象)————————————————————————————————————————————————————大家要知道从Android2.0开始新增了ExifInterface类,ExifInterface类主要描述多媒体文件比如JPG格式图片的一些附加信息,比如拍照的设备厂商,当时的日期时间,曝光时间,快门速度等。该类位于android.media.ExifInterface的位置,需要调用APILevel至少为5即2.0SDK。
一、ExifInterface类给我们了一个实用的方法publicbyte[]getThumbnail()这个方法可以生成一个缩略图,返回一个字节数组,我们通过以前讲到的通过OutputStream将其保存成一个JPG文件。当然如果要是显示到一个Drawable对象,可以通过decodeByteArray(byte[],int,int)类来解析这个字节数组,不过具体生成的分辨率还不清楚。
二、图片的MIMEExif信息
图片的的Exif信息和MP3的ID3标签类似,使用了属性和值的存储方式。通过publicvoidsetAttribute(Stringtag,Stringvalue)来设置,而获取可以通过publicintgetAttributeInt(Stringtag,intdefaultValue)和publicStringgetAttribute(Stringtag)两种方法都可以,getAttributeInt重载方法一第二个参数为我们设置的默认值,如果成功则返回相应Tag的值;特定的整数内容为该方法直接返回值。而重载方法二该方法直接返回结果,如果失败则为null。
目前AndroidSDK定义的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白平衡
从Android2.0.1SDK上看属性不是很多,详细的可能会在以后的版本中添加,调用示例如下
StringsFileName="cwj-eoeandroid.jpg";
ExifInterfaceexif=newExifInterface(filename);
StringsModel=exif.getAttribute(ExifInterface.TAG_MODEL);