Android图片处理:识别图像方向并显示
来源:互联网 发布:武汉c语言周未培训 编辑:程序博客网 时间:2024/06/05 04:17
在Android中使用ImageView显示图片的时候发现图片显示不正,方向偏了或者倒过来了。
解决这个问题很自然想到的分两步走:
1、自动识别图像方向,计算旋转角度;
2、对图像进行旋转并显示。
一、识别图像方向
首先在这里提一个概念EXIF(Exchangeable Image File Format,可交换图像文件),具体解释参见Wiki。
简而言之,Exif是一个标准,用于电子照相机(也包括手机、扫描器等)上,用来规范图片、声音、视屏以及它们的一些辅助标记格式。
Exif支持的格式如下:
图像
压缩图像文件:JPEG、DCT
非压缩图像文件:TIFF
不支持:JPEG 2000、PNG、GIF
音频
RIFF、WAV
Android提供了对JPEG格式图像Exif接口支持,可以读取JPEG文件metadata信息,参见ExifInterface.
这些Metadata信息总的来说大致分为三类:日期时间、空间信息(经纬度、高度)、Camera信息(孔径、焦距、旋转角、曝光量等等)。
二、图像旋转
Android中提供了对Bitmap进行矩阵旋转的操作,参见Bitmap提供的静态createBitmap方法.
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Added in API level 1Returns 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 subsettingx The x coordinate of the first pixel in sourcey The y coordinate of the first pixel in sourcewidth The number of pixels in each rowheight The number of rowsm Optional matrix to be applied to the pixelsfilter 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.
到此这两个问题理论上都解决了,开始实际操作一下吧,参照以下代码。
- public class IOHelper {
- ......
- /** 从给定路径加载图片*/
- public static Bitmap loadBitmap(String imgpath) {
- return BitmapFactory.decodeFile(imgpath);
- }
- /** 从给定的路径加载图片,并指定是否自动旋转方向*/
- public static Bitmap loadBitmap(String imgpath, boolean adjustOritation) {
- if (!adjustOritation) {
- return loadBitmap(imgpath);
- } else {
- Bitmap bm = loadBitmap(imgpath);
- int digree = 0;
- ExifInterface exif = null;
- try {
- exif = new ExifInterface(imgpath);
- } catch (IOException e) {
- e.printStackTrace();
- exif = null;
- }
- if (exif != null) {
- // 读取图片中相机方向信息
- int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
- ExifInterface.ORIENTATION_UNDEFINED);
- // 计算旋转角度
- switch (ori) {
- case ExifInterface.ORIENTATION_ROTATE_90:
- digree = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- digree = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- digree = 270;
- break;
- default:
- digree = 0;
- break;
- }
- }
- if (digree != 0) {
- // 旋转图片
- Matrix m = new Matrix();
- m.postRotate(digree);
- bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
- bm.getHeight(), m, true);
- }
- return bm;
- }
- }
- ......
- }
- Android图片处理:识别图像方向并显示
- Android图片处理:识别图像方向并显示
- Android图片处理:识别图像方向并显示
- Android图片处理:识别图像方向并显示
- 识别图像方向并显示
- opencv图像处理2--打开图片并显示
- android 字符显示方向处理
- 【Android图像处理】从相册选择一张图片并分享
- android图片显示的方向问题
- 图像处理研究方向
- 图像处理研究方向
- 图像处理研究方向
- OpenCV-009:识别摄像头图像中的指定颜色并显示
- 机器学习处理图片数据并识别图片情感
- Android获取并显示图片
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理------相似图片识别(直方图应用篇)
- 《图解TCP/IP》读书笔记(1)
- Logback常用配置详解
- [剑指offer]二叉树中和为某一值的路径
- JENKINS+MAVEN+GIT+TOMCAT自动构建部署记录
- 在opencv中训练cascade分类器,出现Train dataset for temp stage can not be filled
- Android图片处理:识别图像方向并显示
- 推荐系统:协同过滤算法简介
- [面经]豌豆荚三面
- git push错误 remote: error: refusing to update checked out branch: refs/heads/master 解决方式
- 技术资源、资料、知识、书籍收集
- ihistorian Excel add-in
- Js怎么调用外部Js
- VS/VC中各种变量在内存中占多少个字节!
- 19个安全专家一定要关注的开源 GitHub 项目