Bitmap了解和尝试压缩

来源:互联网 发布:浙江省进出口贸易数据 编辑:程序博客网 时间:2024/06/16 22:34

图片优化

一直以来Bitmap都是开发中很棘手的问题,今天我就遇到了,真的很难解决。

处理图片引发这个OOM的原因:
第一,每一个应用都给予了最大可用内存,根据手机屏幕dpi大小递增,dpi越小的手机,每个应用可用最大内存就越低。
第二,就是图片的分辨率,分辨率越高,越耗内存,当加载高分辨率图片的时候,将会非常占用内存,一旦处理不当就会OOM。
第三,在使用ListView, GridView等这些大量加载view的组件时,如果没有合理的处理缓存,大量加载Bitmap的时候,也将容易引发OOM

Bitmap介绍
一张图片Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数

BitmapConfig

A代表透明度;R代表红色;G代表绿色;B代表蓝色。

ALPHA_8:
表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度

ARGB_4444:
表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节

ARGB_8888:
表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节

RGB_565:
表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节

Bitmap.Config主要作用是:以何种方式像素存储。不同的配置将会影响图像的画质(色彩深度),位数越高画质越高,显然在这里ARGB_8888是最占内存的。当然,画质越高也就越占内存了。

一张图片Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数

Bitmap.Config分辨率100x100的图片占用内存的大小ALPHA_8100x100x1 = 10000 byte ~= 9.77 KBARGB_4444100x100x2 = 20000 byte ~= 19.53 kbARGB_8888100x100x4 = 40000 byte ~= 39.06 KBRGB_565100x100x2 = 20000 byte ~= 19.53 KB

在代码中是这样设置的 options.inPreferredConfig = Bitmap.Config.RGB_565;

Bitmap.CompressFormat

Bitmap压缩格式

Bitmap.CompressFormat

嗯,其实这个参数很简单,就是指定Bitmap是以JPEG、PNG还是WEBP格式来压缩
Bitmap.CompressFormat.JPEG
Bitmap.CompressFormat.PNG
Bitmap.CompressFormat.WEBP

Bitmap.compress()方法

使用该方法需要传三个参数进去:CompressFormat、int类型的quality、OutputStream

CompressFormat指定Bitmap的压缩格式,可选择JPEG、PNG、WEBPint类型的quality指定Bitmap的压缩品质,范围是0 ~ 100;该值越高,画质越高。0表示画质最差,100画质最高。OutputStream指定Bitmap的字节输出流。一般使用:ByteArrayOutputStream stream = new ByteArrayOutputStream();

使用方法

// Bitmap.compress()方法public boolean compress(CompressFormat format, int quality, OutputStream stream) {    if (stream == null) {        throw new NullPointerException();    }    if (quality < 0 || quality > 100) {        throw new IllegalArgumentException("quality must be 0..100");    }    Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress");    boolean result = nativeCompress(mFinalizer.mNativeBitmap, format.nativeInt,            quality, stream, new byte[WORKING_COMPRESS_STORAGE]);    Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);    return result;}

example

protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_test3);    ImageView iv = (ImageView) findViewById(R.id.iv);    ByteArrayOutputStream bos = new ByteArrayOutputStream();    bmp.compress(Bitmap.CompressFormat.JPEG, 10, bos);    byte[] bytes = bos.toByteArray();    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);    iv.setImageBitmap(bmp);}

BitmapFactory

从上面那个案例的代码可以发现,获取Bitmap不是通过构造new出来的,而是通过BitmapFactory创建出来的。BitmapFactory是获取Bitmap和压缩Bitmap的重要类,下面开始介绍BitmapFactory几个重要的成员变量和方法:

通过BitmapFactory解码(获取)Bitmap的几种方式decodeFile()   //从SD卡文件读取Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/photo.jpg");decodeResource()   //从资源文件res读取Bitmap bm = BitmapFactory.decodeResource(this.getResources(), R.mipmap.test_pic);decodeStream()   //从输入流读取Bitmap bm = BitmapFactory.decodeStream(inputStream);decodeByteArray()   //从字节数组读取Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

BitmapFactory.Options

BitmapFactory在使用方法decodeFile()、decodeResource()解码图片时,可以指定它的BitmapFactory.Options。这个参数作用非常大,它可以设置Bitmap的采样率,通过改变图片的宽度、高度、缩放比例等,以达到降低图片的像素的目的,这样可以做到图片压缩,减少Bitmap的内存。

in开头的代表的就是设置某某参数;out开头的代表的就是获取某某参数

inJustDecodeBounds 设置只去读图片的附加信息(宽高),不去解析真实的Bitmap

当inJustDecodeBounds设置为true的时候,BitmapFactory通过decodeResource或者decodeFile解码图片时,将会返回空(null)的Bitmap对象,这样可以避免Bitmap的内存分配,但是它可以返回Bitmap的宽度、高度以及MimeType。

    // 当inJustDecodeBounds设置为true时,获取Bitmap的宽度、高度以及MimeTypeBitmapFactory.Options options =  new BitmapFactory.Options(); options.inJustDecodeBounds =  true ; BitmapFactory.decodeResource (getResources(), R.id.myimage, options); int imageHeight = options.outHeight ; int imageWidth = options.outWidth ; String imageType = options.outMimeType ;options.inJustDecodeBounds =  false ; 

通过BitmapFactory.Options根据手机屏幕尺寸设置图片的缩放比例

// 根据手机屏幕尺寸设置图片的缩放比例【将大图缩放】public class TestThreadActivity3 extends Activity {@TargetApi(Build.VERSION_CODES.KITKAT)@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_test3);    ImageView iv_1 = (ImageView) findViewById(R.id.iv_1);    BitmapFactory.Options opts = new BitmapFactory.Options();    opts.inJustDecodeBounds = true;  //只去读图片的头信息,不去解析真实的位图          Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),     R.mipmap.test_pic2,opts);    WindowManager wm  = getWindowManager();    int screenWidth = wm.getDefaultDisplay().getWidth();//得到屏幕的宽度    int screenheight = wm.getDefaultDisplay().getHeight();//得到屏幕的高度    Log.e("屏幕宽度:",screenWidth+"");    Log.e("屏幕高度:", screenheight + "");    int picWidth = opts.outWidth;// 得到图片宽度    int picHeight = opts.outHeight;// 得到图片高度    Log.e("原图片高度:",picHeight+"");    Log.e("原图片宽度:", picWidth + "");    //计算图片缩放比例    int dx = picWidth/screenWidth;    int dy = picHeight/screenheight;    Log.e("dx,dy",dx+","+dy+"");    int scale = 1;    if(dx>=dy&&dy>=1){        Log.e("按照水平方向缩放:" ,dx+"");        scale = dx;    }    if(dy>dx&&dx>=1){        Log.e("按照竖直方向缩放:", dy + "");        scale = dy;    }    opts.inSampleSize = scale;//设置缩放比例    opts.inJustDecodeBounds = false;//真正的去解析位图    bmp = BitmapFactory.decodeResource(this.getResources(), R.mipmap.test_pic2,opts);    int picWidth2 = opts.outWidth;// 得到图片宽度    int picHeight2 = opts.outHeight;// 得到图片高度    Log.e("压缩后的图片宽度:",picWidth2+"");    Log.e("压缩后的图片高度:", picHeight2 + "");    Log.e("压缩后的图占用内存:",bmp.getByteCount()+"");    iv_2.setImageBitmap(bmp);}}

inSampleSize 设置图片的缩放比例(宽和高)

在这里着重讲一下这个inSampleSize。从字面上理解,它的含义是:”设置取样大小“。它的作用是:设置inSampleSize的值(int类型)后,假如设为4,则宽和高都为原来的1/4,宽高都减少了,自然内存也降低了。
0 0