[Android] 旋转照片/图片

摘要:
我今天不忙,所以我将再更新几篇文章,以弥补我之前所做的工作。测试后发现,无论我是正面拍摄还是反面拍摄,任何设备中的图片浏览器都可以从正确的方向读取图片,因此我认为图片中一定有一些信息,这是标准信息,可以指示图片的正方向。另一个需要知道的是如何旋转图像数据。方法描述:Bitmapandroid.graphics.Bitmap。创建位图从子位图返回到源位图的不可更改位图,通过选项矩阵进行转换。新位图可能是一个目标资产,或者可能已经进行了复制。它使用与原始位图相同的标识进行初始化。如果源位图是可变的,并且请求的子集与源位图本身相同,则返回源位图,并且不创建白位图。参数:source位图将观察设置x第一个像素源的坐标第一个像素的坐标宽度右侧的像素数如果要选择源,则会将行数选项应用于像素过滤器。仅当三个像素包含的不仅仅是转换时才应用。返回:Abitmap表示指定的subsetofsourceThrows:IllegalArgumentExceptionifhex,y,width,height值位于源位图的维度外侧。代码如下:读取exif信息,查找图片1 privateinreadPictureDegree{2intdegree=0;3 try{4ExifInterfaceexifInterface=newExifInterface;5 orientation=exifInterface.getAttributeInt;7 switch的正向旋转角度{8caseExifInterface.ORIENTATION_ROTATE_90:9度=90;10折;11caseExifInterface=ORIENTATIION_ROTATE_180:12度=180;13折;14caseExifInterface.ORIENTATION-ROTATE_90 270:15度=270;16折;17}18}捕获{19e.printStackTrace();20}21返回度;找出角度并旋转。让它回到积极的方向。1 privatestaticBitmaprotate{2if{3returnb;4}5if(度!=b2){11b.循环();12b=b2;13}14}捕获{15}16}17返回b;18} 这基本上没有问题。。

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

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

推荐阅读:

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信息,找图片正方向的旋转角度

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

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

1     private static Bitmap rotate(Bitmap b, intdegrees) {
2         if (degrees == 0) {
3             returnb;
4 }
5         if (degrees != 0 && b != null) {
6             Matrix m = newMatrix();
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         returnb;
18     }

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

免责声明:文章转载自《[Android] 旋转照片/图片》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ArcGIS统计栅格像元值并转换为矢量图层mongodb使用实践---mongodb+mongo-java-driver+morphia下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

内存泄漏解析

永远的Singleton 单例的使用在我们的程序中随处可见,因为使用它可以完美的解决我们在程序中重复创建对象的问题,不过可别小瞧它。由于单例的静态特性,使得它的生命周期和应用的生命周期会一样长,所以一旦使用有误,小心无限制的持有Activity的引用而导致内存泄漏。比如,下面的例子。 public class SingletonBad { pri...

c#数字图像处理(一)Bitmap类、 Bitmapdata类和 Graphics类

Bitmap类、 Bitmapdata类和 Graphics类是C#图像处理中最重要的3个类,如果要用C#进行图像处理,就一定要掌握它们。 1.1 Bitmap类Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成。 因此 Bitmap是用于处理由像素数据定义的图像的对象。 Bitmap类类的主要方法和属性如下: Get Pi...

C# Winform 窗体美化

1 using System; 2 using System.ComponentModel; 3 using System.Diagnostics; 4 using System.Drawing; 5 using System.Drawing.Drawing2D; 6 using System.Runtime.InteropServ...

elasticsearch 性能优化

转载: https://www.cnblogs.com/jajian/p/10465519.html 硬件选择 Elasticsearch(后文简称 ES)的基础是 Lucene,所有的索引和文档数据是存储在本地的磁盘中,具体的路径可在 ES 的配置文件../config/elasticsearch.yml中配置,如下: # ---------------...

Android Xfermode 实战 实现圆形、圆角图片

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42094215,本文出自:【张鸿洋的博客】 1、概述 其实这篇本来准备Android BitmapShader 实战 实现圆形、圆角图片放到一篇里面,结果由于篇幅原因就独立出来了~在很久以前也写过一个利用Xfermode 实现圆形、圆角图片...

c#生成cad缩略图或者图片

struct BITMAPFILEHEADER{public short bfType;public int bfSize;public short bfReserved1;public short bfReserved2;public int bfOffBits;}public static System.Drawing.Image GetDwgImag...