在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 1

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.

到此这两个问题理论上都攻克了,開始实际操作一下吧,參照下面代码。

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群: 66756039
JavaEE群:  361579846

Android图片处理:识别图像方向并显示的更多相关文章

  1. Android tess_two Android图片文字识别

    文字识别一般都用的tesseract-ocr. GitHub:https://github.com/tesseract-ocr/tesseract 而Android对应的比较推荐的有个tess-two ...

  2. Android调用OCR识别图像中的文字

    // CharacterExtractor.java // Copyright (c) 2010 William Whitney // All rights reserved. // This sof ...

  3. android 图片解码显示流程

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jingxia2008/article/details/32327699 问题来源 android 能 ...

  4. Android静态图片人脸识别的完整demo(附完整源码)

    Demo功能:利用android自带的人脸识别进行识别,标记出眼睛和人脸位置.点击按键后进行人脸识别,完毕后显示到imageview上. 第一部分:布局文件activity_main.xml < ...

  5. 【Android】5.2 图像按钮和图片格式

    分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 1.ImageBtton ImageBtton的用法和Button相似,也有Click事件,用法也和Button一 ...

  6. Android实现OCR扫描识别数字图片之图片扫描识别

    [Android实例] Android实现OCR扫描识别数字图片之图片扫描识别 Android可以识别和扫描二维码,但是识别字符串呢? google提供了以下解决方案用的是原来HP的相关资料. 可以吧 ...

  7. Android之拨号界面图片风格,无信息默认显示界面修改

    Android之拨号界面图片风格,无信息默认显示界面修改 点开Dialer app,出现拨号,联系人,收藏三个选项卡,也就是三个Fragment,在三个界面都没有信息的时候会显示一个时钟,联系人,收藏 ...

  8. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  9. Android根据Button状态(normal,focused,pressed)显示不同背景图片

    Android根据Button状态(normal,focused,pressed)显示不同背景图片 Android中Button 有focused, selected, pressed 等不同状态,通 ...

随机推荐

  1. IOS sqlite数据库增删改查

    1.简单介绍 简单封装sqlite数据库操作类 BaseDB 用于完毕对sqlite的增删改查.使用前先导入libsqlite3.0.dylib库 2.BaseDB.h // // BaseDB.h ...

  2. C# Lambda表达式 基础

    什么是Lambda 表达式? "Lambda表达式"实际上是一个方法,只不过该方法是一个匿名方法(就是没有名字的方法(函数),就是说只有在定义的时候能调用,在其他地方就不能调用了) ...

  3. shapefile文件的符号化问题

    我们都知道,ArcGIS的shp文件只以坐标形式保存地图数据,地图的显示方法则是存储都数据库或地图文件(mxd)中,这一点是深信不疑的. 如果我们打开ArcMap,新建一个普通的地图文件(使用标准的模 ...

  4. W3CSchool CSS学习简记

    什么是 CSS?   CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内 ...

  5. Android 之类库常用包

    Android 是由谷歌公司推出的一款基于Linux平台的开源手机操作系统平台. 在Android类库中,各种包写成android.*的方式,重要包的描述如下所示: [1]android.app:提供 ...

  6. java web下串口通讯

       最近在做java串口通讯,主要是用个人电脑通过串口从RS485读取数据,并通过crc循环冗余校验,把接收正确的数据解析,插入数据库mysql,并用SSH技术把数据库数据以表格以及图表形式显示   ...

  7. tomcat之组成结构

    1 - Tomcat Server的组成部分 1.1 - Server A Server element represents the entire Catalina servlet containe ...

  8. SQL Sever 2008配置工具中过程调用失败解决方法

    刚刚装了VS2013.然后打开数据库时,不管怎样也连不上.打开数据库配置,出现例如以下界面: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHU5MzAx ...

  9. 算法笔记_126:算法集训之编程大题集二(Java)

     目录 1 连续数的公倍数 2 漏掉的账目明细 3 罗马数字转十进制 4 逻辑推断 5 平面4点最小距离 6 取球博弈 7 人民币金额大写 8 人员排日程 9 三角螺旋阵 10 手机尾号评分   1 ...

  10. openerp修改logo和title

    LOGO的修改addons\web\static\src\img下面的:favicon.ico文件和logo2.png \addons\web\static\src\xml\base.xml#82 l ...