大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > android旋转照片/图片

android旋转照片/图片

关键词:旋转照片图片android  阅读(535) 赞(18)

[摘要]本文是对android旋转照片/图片的讲解,对学习Android编程技术有所帮助,与大家分享。

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

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

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

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

用createBitmap(Bitmapsource,int x,int y,int width,int height,Matrixm,boolean filter)就好。。

方法说明:

Bitmapandroid.graphics.Bitmap.createBitmap(Bitmapsource,int x,int y,int width,int height,Matrixm,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:
sourceThe bitmap we are subsetting
xThe x coordinate of the first pixel in source
yThe y coordinate of the first pixel in source
widthThe number of pixels in each row
heightThe number of rows
mOptional matrix to be applied to the pixels
filtertrue 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信息,找图片正方向的旋转角度

 private 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;
     }

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

     private static Bitmap rotate(Bitmap b, int degrees) {
         if (degrees == 0) {
             return b;
         }
         if (degrees != 0 && b != null) {
             Matrix m = new Matrix();
             m.setRotate(degrees, (float) b.getWidth(), (float) b.getHeight());
             try {
                 Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
                 if (b != b2) {
                     b.recycle();
                     b = b2;
                 }
             } catch (OutOfMemoryError ex) {
             }
         }
         return b;
     }

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



相关评论