[Android] 旋转照片/图片

本文介绍如何使用Android设备拍照后正确显示图片方向。通过解析图片的EXIF信息确定正确的旋转角度,并利用Bitmap对象进行图像旋转处理。

今天比较闲(是任务做完了,不是偷懒),就多更新几篇,补一下之前做的东西。

原文地址请保留http://www.cnblogs.com/rossoneri/p/3995306.html 

推荐阅读:

Android图片处理:识别图像方向并显示实例教程

android中调用系统相机得到的相片的方向判断

 

做图片旋转前我考虑了一个问题,就是把android设备反着拿拍照,打开照片的时候是不是反着的。测试后发现,不管我是正着拍照还是倒着拍照,在任何(其实是大部分)设备里的图片浏览器都能把照片正着读出来,所以我就想这个照片里肯定有什么信息,而且是标准信息,来表示照片的正方向。

后来查了资料,发现有这么个玩意:EXIF

有标准,就找接口用就是了。。

另外一个需要知道就就是图像数据旋转怎么搞。

用createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)就好。。

方法说明:

Bitmap  android.graphics.Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

 

Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.

Parameters:
source  The bitmap we are subsetting
x  The x coordinate of the first pixel in source
y  The y coordinate of the first pixel in source
width  The number of pixels in each row
height  The number of rows
m  Optional matrix to be applied to the pixels
filter  true if the source should be filtered. Only applies if the matrix contains more than just translation.
Returns:
A bitmap that represents the specified subset of source
Throws:
IllegalArgumentException  - if the x, y, width, height values are outside of the dimensions of the source bitmap.

下面放代码:

读取exif信息,找图片正方向的旋转角度

 1 private int readPictureDegree(String path) {
 2         int degree = 0;
 3         try {
 4             ExifInterface exifInterface = new ExifInterface(path);
 5             int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
 6                     ExifInterface.ORIENTATION_NORMAL);
 7             switch (orientation) {
 8             case ExifInterface.ORIENTATION_ROTATE_90:
 9                 degree = 90;
10                 break;
11             case ExifInterface.ORIENTATION_ROTATE_180:
12                 degree = 180;
13                 break;
14             case ExifInterface.ORIENTATION_ROTATE_270:
15                 degree = 270;
16                 break;
17             }
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21         return degree;
22     }

找出角度就旋转吧,让其转回到正方向显示

 1     private static Bitmap rotate(Bitmap b, int degrees) {
 2         if (degrees == 0) {
 3             return b;
 4         }
 5         if (degrees != 0 && b != null) {
 6             Matrix m = new Matrix();
 7             m.setRotate(degrees, (float) b.getWidth(), (float) b.getHeight());
 8             try {
 9                 Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
10                 if (b != b2) {
11                     b.recycle();
12                     b = b2;
13                 }
14             } catch (OutOfMemoryError ex) {
15             }
16         }
17         return b;
18     }

这个基本上没问题。。也许有的板子会无法读取到正方向吧。

转载于:https://www.cnblogs.com/rossoneri/p/3995306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值