android旋转照片/图片

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

养成好习惯,先推荐阅读:

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     }

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

时间: 2024-08-09 10:34:36

android旋转照片/图片的相关文章

Android实现对图片的缩放、剪切、旋转、存储

Android实现对图片的缩放.剪切.旋转.存储 一.问题描述 在开发中,当我们需要的有一张大图片同时还需要一些小图片时,我们只需要通过代码对此图片进行不同比例的缩放即可,这样大大节约资源,减小了安装包的尺寸 .除缩放外,我们还经常对图片进行其他操作如裁剪.旋转.存储等. 这样我们可以编写对于图片进行处理的通用组件,方便开发.下面就分享一下对图片进行处理的组件BitmapUtil,案例界面: 二.技术点描述 1.通过BitmapFactory取得Bitmap Bitmap bm=BitmapFa

Android中读取图片EXIF元数据之metadata-extractor的使用

一.引言及介绍 近期在开发中用到了metadata-extractor-xxx.jar 和 xmpcore-xxx.jar这个玩意, 索性查阅大量文章了解学习,来分享分享. 本身工作也是常常和处理大图片打交道,摸索摸索也是多多益善. 首先介绍一下什么是EXIF.EXIF是 Exchangeable Image File 的缩写,这是一种专门为数码相机照片设定的格式.这样的格式能够用来记录数字照片的属性信息,如相机的品牌及型号.相片的拍摄时间.拍摄时所设置的光圈大小.快门速度.ISO等信息.除此之

android的照片浏览器(一)至返回所有图片文件

今天开始写android的照片浏览器 首先要解决的问题是要得到sdcard下面所有是图片的文件的目录 于是我先写了一个普通的java类 来得到后缀是.jpg,.bmp.png.jpeg的文件 package com.jiangqq.utlis; import java.io.File; import java.util.ArrayList; public class FileUtils { private static ArrayList filelist = new ArrayList();

Android笔记之 图片自由裁剪

前言--项目中需要用到对用户头像的裁剪和上传功能.关于裁剪,一开始是想自己来做,但是觉得这个东西应该谷歌有开发吧,于是一搜索官方文档,果然有.于是,就果断无耻地用了Android自带有关于照片的自由裁剪.因为时间太紧,虽然不太华丽,但是胜在能用,节省时间嘛. 具体是通过 Intent的action来实现的. 关键代码如下: public void imageCut(Uri uri) { Intent intent = new Intent("com.android.camera.action.C

Android旋转动画

Android旋转动画 核心方法 public void startAnimation(Animation animation) 执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有. 2个参数的构造方法 /** * Constructor to use when building a RotateAnimation from code. * Default pivotX/pivotY point is (0,0). * * @param fromDegree

Android下将图片载入到内存中

Android的系统的标准默认每一个应用程序分配的内存是16M.所以来说是很宝贵的,在创建应用的时候要尽可能的去节省内存,可是在载入一些大的文件的时候,比方图片是相当耗内存的,一个1.3M的图片,分辨率是2560X1920(宽X高)图片当载入到手机内存的时候就会请求19M的一块内存,这是远远超出了系统自带的内存空间,这时候应用程序就会挂掉,所以我们要进行图片的缩放处理,以下我就来带大家创建一个用来图片缩放的应用: 应用效果图例如以下: 核心代码的实现: package com.examp.loa

Android Bitmap 开源图片框架分析(精华三)

主要介绍这三个框架,都挺有名的,其他的框架估计也差不多了 Android-Universal-Image-Loaderhttps://github.com/nostra13/Android-Universal-Image-Loader ImageLoaderhttps://github.com/novoda/ImageLoader Volley(综合框架,包含图片部分)https://github.com/mcxiaoke/android-volley 扯淡时间,可以跳过这段这些开源框架的源码还

Android Bitmap 开源图片框架分析(精华四)

disk缓存主要难点在于内存缓存,disk缓存其实比较简单,就是图片加载完成后把图片文件存到本地方便下次使用 同样,先贴一下官方主页的介绍(主页地址见文章最开始处)和内存缓存差不多,根据算法不同提供了几种类别,可以自行通过ImageLoaderConfiguration.discCache(..)设置<ignore_js_op> 硬盘缓存,保存是以文件的形式框架提供了4种类型,具体算法规则不同,看名字我们大概也能知道对应意思 UnlimitedDiscCache                

Android中的图片压缩

1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB  之所以要乘以4,是因为在android中使用的ARGB图片,图片一个像素占用四个字节. 2.手机出厂时 堆内存(Heap)是固定的,所以为了不造成OOM,我们就需要生成bitmap时对图片进行压缩处理. 实际使用中我们压缩图片的标准是手机屏幕大小作为参照的,这个主要是因为,即便是图片尺寸跟屏幕尺