Android 根据角度旋转图片 ExifInterface

在Android开发过程中,几乎每个应用都会或多或少的涉及到对图片的处理。经常遇到的一个情况就是,取得的图片是横着的,而实际需要的图片是正着的,也就是竖着的。这里就涉及到对图片横坚情况的判断,也就是图片的当前的角度。然后根据角度来纠正,得到想要的图片。

       在Android的源代码里提供了一个专门读写图片信息的类ExifInterface,官方给出的注释为:This is a class for reading and writing Exif tags in a JPEG file ,可见ExifInterface是专门用来读写JPEG图片文件Exif信息的类。

        Exif信息里面就包括了角度,GPS经纬度,白平衡,闪光灯等信息。ExifInterface的用法相对简单,只有一个带参的构造方法,将图片文件地址传过去就可以了。类里提供了getAttribute方法来取得各种属性,当得也可以用setAttribute方法来为已存在的图片设置或修改其本来属性。

       下面贴上代码:

  1. /** 
  2.  * 读取图片属性:旋转的角度 
  3.  * @param path 图片绝对路径 
  4.  * @return degree旋转的角度 
  5.  */  
  6. public static int readPictureDegree(String path) {  
  7.     int degree = 0;  
  8.     try {  
  9.         ExifInterface exifInterface = new ExifInterface(path);  
  10.         int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);  
  11.         switch (orientation) {  
  12.         case ExifInterface.ORIENTATION_ROTATE_90:  
  13.             degree = 90;  
  14.             break;  
  15.         case ExifInterface.ORIENTATION_ROTATE_180:  
  16.             degree = 180;  
  17.             break;  
  18.         case ExifInterface.ORIENTATION_ROTATE_270:  
  19.             degree = 270;  
  20.             break;  
  21.         }  
  22.     } catch (IOException e) {  
  23.         e.printStackTrace();  
  24.         return degree;  
  25.     }  
  26.     return degree;  
  27. }  

         能过以上方法得到图片角度后,就可以通过Matrix类对图片进行纠正了,还是贴上完整的代码,如下:

  1. /** 
  2.  * 旋转图片,使图片保持正确的方向。 
  3.  * @param bitmap 原始图片 
  4.  * @param degrees 原始图片的角度 
  5.  * @return Bitmap 旋转后的图片 
  6.  */  
  7. public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {  
  8.     if (degrees == 0 || null == bitmap) {  
  9.         return bitmap;  
  10.     }  
  11.     Matrix matrix = new Matrix();  
  12.     matrix.setRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2);  
  13.     Bitmap bmp = Bitmap.createBitmap(bitmap, 00, bitmap.getWidth(), bitmap.getHeight(), matrix, true);  
  14.     if (null != bitmap) {  
  15.         bitmap.recycle();  
  16.     }  
  17.     return bmp;  
  18. }  

        通过以上两个步骤,就可以得到一个正着的图片了。当然中间省略了一步:

      

  1. Bitmap bmp =BitmapFactory.decodeFile(imageFilePath); ————————————————————————————————————————



   在android多媒体开发中,ExifInterface(exif exchangeable image file) ,这个接口提供了图片文件的旋转,gps,时间等信息。

  1. Bitmap bitmap =null;  
  2.        int scallType = 0;  
  3.        try {   
  4.            ExifInterface exifInterface = new ExifInterface(file.getPath());   
  5.            int result = exifInterface.getAttributeInt(   
  6.                    ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);   
  7.            int rotate = 0;   
  8.            switch(result) {   
  9.            case ExifInterface.ORIENTATION_ROTATE_90:   
  10.                rotate = 90;   
  11.                break;   
  12.            case ExifInterface.ORIENTATION_ROTATE_180:   
  13.                rotate = 180;   
  14.                break;   
  15.            case ExifInterface.ORIENTATION_ROTATE_270:   
  16.                rotate = 270;   
  17.                break;   
  18.            default:   
  19.                break;   
  20.            }   
  21.            BitmapFactory.Options options = new BitmapFactory.Options();  
  22.            options.inPreferredConfig = Bitmap.Config.RGB_565;  
  23.            // 初めのデコードはサイズ取得のみ  
  24.            options.inJustDecodeBounds = true;  
  25.            BitmapFactory.decodeFile(filePath, options);  
  26.            if (options.outWidth < 0 || options.outHeight < 0) {  
  27.                return null;  
  28.            }  
  29.               
  30.            scallType = genScallType(context, options);  
  31.              
  32.            options.inJustDecodeBounds = false;  
  33.            bitmap=  BitmapFactory.decodeFile(filePath, options);  
  34.            if(rotate > 0) {   
  35.                Matrix matrix = new Matrix();   
  36.                matrix.setRotate(rotate);   
  37.                Bitmap rotateBitmap = Bitmap.createBitmap(   
  38.                        bitmap, 00, options.outWidth, options.outHeight, matrix, true);   
  39.                if(rotateBitmap != null) {   
  40.                    bitmap.recycle();   
  41.                    bitmap = rotateBitmap;   
  42.                }   
  43.            }   
  44.        } catch (IOException e) {   
  45.            e.printStackTrace();   
  46.        }   


 

public int getAttributeInt(String tag, int defaultValue)
Since: API Level 5

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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值