[Android] 旋转照片/图片

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

原文地址请保留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     }

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

posted @ 2014-09-26 17:23  Wossoneri  阅读(1332)  评论(0编辑  收藏  举报