Bitmap 二次采样

作者: 菩提大师 | 来源:发表于2017-02-22 09:49 被阅读33次

Bitmap二次采样

一、二次采样:

(一)、意义或目的:

1、用BitmapFactory解码一张图片时,有时会遇到该错误。这往往是由于图片过大造成的。要想正常使用,则需要分配更少的内存空间来存储。BitmapFactory.decodeFile(imageFile);

2、BitmapFactory.Options.inSampleSize:设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误。

3、BitmapFactory.Options提供了另一个成员inJustDecodeBounds。设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。

例如:

BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inSampleSize = 4;

Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

(二)、获取inSampleSize:

设置恰当的inSampleSize是解决该问题的关键之一。

查看Android源码,我们得知,为了得到恰当的inSampleSize,Android提供了一种动态计算的方法。

1、核心代码:

privateBitmap createImageThumbnail(String filePath,intnewHeight,

intnewWidth) {

BitmapFactory.Options options =newBitmapFactory.Options();

options.inJustDecodeBounds=true;

BitmapFactory.decodeFile(filePath, options);

intoldHeight = options.outHeight;

intoldWidth = options.outWidth;

// Log.i(TAG, "高度是:" + oldHeight + ",宽度是:" + oldWidth);

intratioHeight = oldHeight / newHeight;

intratioWidth = oldWidth / newWidth;

options.inSampleSize= ratioHeight > ratioWidth ? ratioWidth

: ratioHeight;

options.inPreferredConfig= Config.RGB_565;

options.inJustDecodeBounds=false;

Bitmap bm = BitmapFactory.decodeFile(filePath, options);

// Log.i(TAG, "高度是:" + options.outHeight + ",宽度是:" + options.outWidth);

returnbm;

}

二、Bitmap占用内存的计算:

(一)、概述:

Android中一张图片(Bitmap)占用的内存主要和以下几个因数有关:图片长度,图片宽度,单位像素占用的字节数。一张图片(Bitmap)占用的内存=图片长度*图片宽度*单位像素占用的字节数注:图片长度和图片宽度的单位是像素。图片(Bitmap)占用的内存应该和屏幕密度(Density)无关。

创建一个Bitmap时,其单位像素占用的字节数由其参数BitmapFactory.Options的inPreferredConfig变量决定。

(二)、inPreferredConfig为Bitmap.Config类型,Bitmap.Config类是个枚举类型,它可以为以下值 Enum Values:

1、Bitmap.Config  ALPHA_8 :

Each pixel is stored as a single translucency (alpha) channel.This is very useful to efficiently store masks for instance. No color information is stored.With this configuration, each pixel requires 1 byte of memory.

此时图片只有alpha值,没有RGB值,一个像素占用一个字节。

2、Bitmap.Config ARGB_4444 :

This field is deprecated. Because of the poor quality of this configuration,It is advised to use ARGB_8888 instead.

这种格式的图片,看起来质量太差,已经不推荐使用。 而强烈推荐使用ARGB_8888来代替。

Each pixel is stored on 2 bytes. The three RGB color channels and the alpha channel (translucency)are stored with a 4 bits precision (16 possible values.)This configuration is mostly useful if the application needs to store translucency informationbut also needs to save memory. It is recommended to use ARGB_8888 instead of this configuration.

一个像素占用2个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占4个bites 。共16bites,即2个字节。

3、Bitmap.ConfigARGB_8888:

Each pixel is stored on 4 bytes. Each channel (RGB and alpha for translucency)is stored with 8 bits of precision (256 possible values.) This configurationis very flexible and offers the best quality. It should be used whenever possible。

一个像素占用4个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占8个bites ,共32bites , 即4个字节。这是一种高质量的图片格式,电脑上普通采用的格式。它也是Android手机上一个BitMap的默认格式。

4、Bitmap.ConfigRGB_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 blueis stored with 5 bits of precision. This configuration can produce slight visualartifacts depending on the configuration of the source. For instance,without dithering, the result might show a greenish tint.To get better results dithering should be applied.This configuration may be useful when using opaque bitmaps that do not require high color fidelity.

一个像素占用2个字节,没有alpha(A)值,即不支持透明和半透明,Red(R)值占5个bites ,Green(G)值占6个bites ,Blue(B)值占5个bites,共16bites,即2个字节。对于半透明颜色的图片来说,该格式的图片能够达到比较好的呈现效果,相对于ARGB_8888来说也能减少一半的内存开销,因此它是一个不错的选择。

另外我们通过android.content.res.Resources来取得一个张图片时,它也是以该格式来构建BitMap。从Android4.0开始,该选项无效。即使设置为该值,系统任然会采用 ARGB_8888来构造图片。

【备注】:ARGB指的是一种色彩模式,里面A代表Alpha,R表示red,G表示green,B表示blue,其实所有的可见色都是红绿蓝组成的,所以红绿蓝又称为三原色。 ARGB就是:透明度 红色 绿色 蓝色。

(三)、图片格式占用内存的计算方法:以一张100*100px的图片占用内存为例

ALPHA_8:

图片长度*图片宽度

100*100=10000字节

ARGB_4444:

图片长度*图片宽度*2

100*100*2=20000字节

ARGB_8888:

图片长度*图片宽度*4

100*100*4=40000字节

RGB_565:

图片长度*图片宽度*2

100*100*2=20000字节

在Android SDK中支持的图片格式如下:png (preferred), jpg (acceptable), gif (discouraged)

相关文章

  • Bitmap 二次采样

    Bitmap二次采样 一、二次采样: (一)、意义或目的: 1、用BitmapFactory解码一张图片时,有时会...

  • Android Bitmap二次采样

    Bitmap二次采样 一、二次采样: (一)、意义或目的: 1、用BitmapFactory解码一张图片时,有时会...

  • Android回顾--(二十六) Bitmap二次采样

    Bitmap的二次采样 一、二次采样 (一)、意义和目的 用BitmapFactory解码有一张图片时,有时会遇到...

  • Android之Bitmap的二次采样

    其实Bitmap的二次采样就那么回事,别怕,我们现在来鸡婆它! 1、为什么要二次采样Android app开发过程...

  • Bitmap二次采样

    1、为什么要二次采样 不知道大家在开发App的过程中有没有遇到过类似于图片墙这样的功能?在做图片墙的时候你有没有遇...

  • Bitmap二次采样

    java代码: 工具类: BitmapFactory在解码图片时,可以带一个Options,有一些比较有用的功能,...

  • BitMap压缩以及二次采样

    BitMap压缩以及二次采样 标签: Android 首先我们来了解一下什么是oom? 1.什么是OOM?为什么会...

  • Bitmap图片处理,二次采样

    java代码: 工具类: BitmapFactory在解码图片时,可以带一个Options,有一些比较有用的功能,...

  • bitmap二次采样和图片模式

    二次采样 日常开发中,有时会需要加载体积比较大的图片,而实际上imageView的大小也往往不足以显示图片文件的所...

  • Android Bitmap优化

    Bitmap 的采样压缩(这个采样率是根据需求来进行生成的),使用到了inBitmap内存复用和 inJustDe...

网友评论

    本文标题:Bitmap 二次采样

    本文链接:https://www.haomeiwen.com/subject/mxbpwttx.html