2014-11-6Android学习------Android图像处理之Bitmap类

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。本文从应用的角度,着重介绍怎么用Bitmap来实现这些功能。

一、Bitmap的生成

1.1 BitmapFactory decode出Bitmap

    Bitmap实现在android.graphics包中。但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化。这必然是 某个辅助类提供了创建Bitmap的接口,而这个类的实现通过JNI接口来实例化Bitmap的,这个类就是BitmapFactory。

例如这样的调用方法:

mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.RGB_565);

这跟设计模式的单一模式有点相同。由Bitmap自己去负责创造,类中有这个静态方法,这个静态方法的返回对象是Bitmap

static  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.
static  Bitmap createBitmap(int width, int height,  Bitmap.Config config)
Returns a mutable bitmap with the specified width and height.
static  Bitmap createBitmap( Bitmap source, int x, int y, int width, int height)
Returns an immutable bitmap from the specified subset of the source bitmap.
static  Bitmap createBitmap(int[] colors, int offset, int stride, int width, int height,  Bitmap.Config config)
Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
static  Bitmap createBitmap( Bitmap src)
Returns an immutable bitmap from the source bitmap.
static  Bitmap createBitmap(int[] colors, int width, int height,  Bitmap.Config config)
Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
static  Bitmap createScaledBitmap( Bitmap src, int dstWidth, int dstHeight, boolean filter)
Creates a new bitmap, scaled from an existing bitmap.
BitmapFactory类图关系:

Android图像处理之Bitmap类

图一、BitmapFactory主要方法及Options选项

这种创造Bitmap的方法是用位图工厂来创建,主要就是解析文件或者解析资源文件,也就是从一个现有的图片来创造出位图对象,或者用资源id来创建

利用 BitmapFactory 可以从一个指定文件中,利用 decodeFile ()解出Bitmap;也可以定义的图片资源中,利用 decodeResource ()解出Bitmap。

从上面的类图中我们首先需要去了解下Bitmap.Config 的常量类型有哪些:

Enum Values
Bitmap.Config  ALPHA_8  Each pixel is stored as a single translucency (alpha) channel. 
Bitmap.Config  ARGB_4444  This field is deprecated. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead.  
Bitmap.Config  ARGB_8888  Each pixel is stored on 4 bytes. 
Bitmap.Config  RGB_565  Each pixel is stored on 2 bytes and only the RGB channels are encoded: red is stored with 5 bits of precision (32 possible values), green is stored with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision. 
Config ALPHA_8 每个像素存储为单个半透明(阿尔法)信道。
Config ARGB_4444 此字段已被弃用。因为这个结构质量差,建议使用ARGB_8888代替。
ARGB_8888 每个像素上存储的4个字节。
RGB_565 每个像素被存储在2个字节,并仅仅将RGB通道进行编码:红色存储与5位的精度(32个可能的值),绿色存储有6比特的精度(64个可能的值)和蓝色被存储用5位的精度。

		/*
		 * android.graphics.Bitmap.Config是一个枚举类型,里面定义了位图的四种格式
		 * ALPHA_8:数字为8,图形参数应该由一个字节来表示,应该是一种8位的位图
		 * ARGB_4444:4+4+4+4=16,图形的参数应该由两个字节来表示,应该是一种16位的位图.
		 * ARGB_8888:8+8+8+8=32,图形的参数应该由四个字节来表示,应该是一种32位的位图.
		 * RGB_565:5+6+5=16,图形的参数应该由两个字节来表示,应该是一种16位的位图.
		 * 
		 * 网上讲,ALPHA_8,ARGB_4444,ARGB_8888都是透明的位图,也就是所字母A代表透明.
		 * ARGB_4444:意味着有四个参数,即A,R,G,B,每一个参数由4bit表示. 同理:
		 * ARGB_8888:意味着有四个参数,即A,R,G,B,每一个参数由8bit来表示. 同理:
		 * RGB_565:意味着有三个参数,R,G,B,三个参数分别占5bit,6bit,5bit.
		 */

接下来我们需要看看BitmapFactory.Options都有哪些 分别代表什么意思:

decode时的选项

在使用方法decodeFile()/decodeResource()时,都可以指定一个BitmapFacotry.Options

利用Options的下列属性,可以指定decode的选项:

  • inPreferredConfig 指定decode到内存中,手机中所采用的编码,可选值定义在Bitmap.Config中。缺省值是ARGB_8888。
  • inJustDecodeBounds 如果设置为true,并不会把图像的数据完全解码,亦即decodeXyz()返回值为null,但是Options的outAbc中解出了图像的基本信息。
  • inSampleSize 设置decode时的缩放比例。

利用Options的这些值就可以高效的得到一幅缩略图。

Android图像处理之Bitmap类

图二、BitmapFactory.decodeFile()

先设置inJustDecodeBounds= true,调用decodeFile()得到图像的基本信息[Step#2~4];

利用图像的宽度(或者高度,或综合)以及目标的宽度,得到inSampleSize值,再设置inJustDecodeBounds= false,调用decodeFile()得到完整的图像数据[Step#5~8]。

先获取比例,再读入数据,如果欲读入大比例缩小的图,将显著的节约内容资源。有时候还会读入大量的缩略图,这效果就更明显了。

二、利用Bitmap和Matrix实现图像变换

    Bitmap可以和Matrix结合实现图像的剪切、旋转、缩放等操作。

Android图像处理之Bitmap类

图三、Bitmap方法

用源Bitmap通过变换生成新的Bitmap的方法:

?
1
2
3
4
5
public static Bitmap createBitmap(Bitmap source,  int x,  int y, intwidth,  int height, 
             Matrix m,  boolean filter) 
public static Bitmap createBitmap(Bitmap source,  int x,  int y, intwidth,  int height) 
public static Bitmap createScaledBitmap(Bitmap src,  int dstWidth, 
             int dstHeight, boolean filter)

第一个方法是最终的实现,后两种只是对第一种方法的封装。

第二个方法可以从源Bitmap中指定区域(x,y, width, height)中挖出一块来实现剪切;第三个方法可以把源Bitmap缩放为dstWidth x dstHeight的Bitmap。

 

设置Matrix的Rotate(通过setRotate())或者Scale(通过setScale()),传入第一个方法,可实现旋转或缩放。

Android图像处理之Bitmap类

图四、Bitmap实现旋转

 

三、保存图像文件

    经过图像变换之后的Bitmap里的数据可以保存到图像压缩文件里(JPG/PNG)。

Android图像处理之Bitmap类

图五、保存Bitmap数据到文件

 

这个操作过程中,Bitmap.compress()方法的参数format可设置JPEG或PNG格式;quality可选择压缩质量;fOut是输出流(OutputStream),这里的FileOutputStream是OutputStream的一个子类。

原文地址: http://www.open-open.com/lib/view/open1333418945202.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值