美文网首页
Android 图片压缩

Android 图片压缩

作者: 磨磨唧唧儿童杰 | 来源:发表于2017-02-06 22:04 被阅读0次

android图的压缩分三种

1.质量压缩

2.比例压缩

3.减少像素占的位数压缩图片

质量压缩

质量压缩就是对图片文件的大小进行压缩,而图片转化为bitmap占用的内存并不会改变。


public staticBitmapcompressBitmap(Bitmap bitmap) {

Log.e("size",bitmap.getByteCount()/1024+"kb");

ByteArrayOutputStream stream =newByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);//质量压缩方法,100代表不压缩,数据传到stream

intoptions =90;

while(stream.toByteArray().length/1024>2048) {//如果图片大于2048kb继续压缩

stream.reset();//清空输出流中的数据

bitmap.compress(Bitmap.CompressFormat.JPEG,options,stream);

options -=10;

}

Log.e("size",stream.toByteArray().length/1024+"kb");

ByteArrayInputStream inputStream =newByteArrayInputStream(stream.toByteArray());

Bitmap compressBitmap= BitmapFactory.decodeStream(inputStream, null, null);

Log.e("size",compressBitmap.getByteCount()/1024+"kb");

returncompressBitmap;

}

通过上面的代码可以看出图片占用的运行内存并没改变。但是stream却变小了。

比例压缩

比例压缩改变图片的分辨率,减少图的像素点。牺牲图片了图片的清晰度。


public staticBitmapgetBitmap(String srcPath) {

BitmapFactory.Options options =newBitmapFactory.Options();

options.inJustDecodeBounds=true;//不给bitmap分配内存,但图片宽高信息存在了options中

Bitmap bitmap = BitmapFactory.decodeFile(srcPath,options);

intw = options.outWidth;

inth = options.outHeight;

floathh =800f;

floatww =480f;

intbe =1;// be=1表示不缩放

if(w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放

be = (int) (options.outWidth/ ww);

}else if(w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放

be = (int) (options.outHeight/ hh);

}

if(be <=0) {

be =1;

}

options.inSampleSize= be;//设置缩放比例

options.inJustDecodeBounds=false;

bitmap = BitmapFactory.decodeFile(srcPath,options);

return bitmap;

}```

打印查看bitmap占用的内存大小,会发现占用内存变小了

减少像素占的位数压缩图片

Bitmap.Config ARGB_4444:由4个4位组成,即A=4,R=4,G=4,B=4,那么一个像素点占4+4+4+4=16位

Bitmap.Config ARGB_8888:由4个8位组成,即A=8,R=8,G=8,B=8,那么一个像素点占8+8+8+8=32位

Bitmap.Config RGB_565:即R=5,G=6,B=5,没有透明度,那么一个像素点占5+6+5=16位

Bitmap.Config ALPHA_8:只有透明度,没有颜色,那么一个像素点占8位。

所以

ALPHA_8代表8位Alpha位图

ARGB_4444代表16位ARGB位图

ARGB_8888代表32位ARGB位图

RGB_565代表16位RGB位图

位图位数越高代表其可以存储的颜色信息越多,图像也就越逼真

private static final intRGB_565=0x01;

private static final intALPHA_8=0x02;

private static final intARGB_4444=0x03;

public staticBitmapgitImage(String path, inttype) {

BitmapFactory.Options options =newBitmapFactory.Options();

switch(type) {

caseRGB_565:

options.inPreferredConfig= Bitmap.Config.RGB_565;

break;

caseALPHA_8:

options.inPreferredConfig= Bitmap.Config.ALPHA_8;

break;

caseARGB_4444:

options.inPreferredConfig= Bitmap.Config.ARGB_4444;

break;

default:

options.inPreferredConfig= Bitmap.Config.ARGB_8888;

break;

}

Bitmap bitmap = BitmapFactory.decodeFile(path,options);

ByteArrayOutputStream stream =newByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);

Log.d("size",stream.toByteArray().length/1024+"kb");

returnbitmap;

}

相关文章

  • 图片压缩

    Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法、比例压缩法(根据路径获取图片并压缩)和比例压缩...

  • Android:图片压缩的几种方式

    1、前言 在Android中,图片的压缩对于内存的优化很重要 通过这篇文章对Android中的几种图片压缩方式进行...

  • 图片压缩

    Android图片压缩常用的有质量压缩、尺寸压缩、采样率压缩以及通过JNI调用libjpeg库来进行压缩(尺寸压缩...

  • 【Android开发基础系列】图片专题

    1 图片编辑处理 1.1 图片裁切 转载自:bitmap的六种压缩方式,Android图片压缩 http://bl...

  • 资源图片优化

    tinypng 在线图片压缩 webP android studio选中图片右键Convert to webP

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • bitmap的六种压缩方式,Android图片压缩

    此处分享一个图片的压缩处理方式 : bitmap的六种压缩方式,Android图片压缩 转载链接,点击查看详情 !

  • Android图片压缩

    1.采样率压缩 采样率压缩是改变了图片的像素,他是通过先读取图片的边,然后在自己设定图片的边,然后根据设定,读取图...

  • android图片压缩

    质量压缩 尺寸压缩 缩放法压缩(matrix) RGB_565法(比ARGB_888少一半) createScal...

  • Android图片压缩

    首先,需要注意的是图片在内存中的大小是根据分辨率来决定的,即height*width BitmapFactory这...

网友评论

      本文标题:Android 图片压缩

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