美文网首页
Android 图片二次采样、质量压缩

Android 图片二次采样、质量压缩

作者: 天天大保建 | 来源:发表于2017-09-21 19:35 被阅读0次

引言:

Android系统支持几种图片(.png (preferred), .jpg (acceptable), .gif (discouraged)), 其中Bitmap位图#ffffffff,包括图片透明度Alpha和RGB,图片质量很好,每一个像素位占4个字节,如果图片很大将会占据很大的内存空间。存储在SDCard的image很小,加载进内存可能就会很大。因此,对bitmap图像进行操作,应该特别小心,可能出现内存溢出问题。为此对于大图片,应该引入该图片的二次采样生成缩略图。

一、图片二次采样

/**

* 根据图片字节数组,对图片可能进行二次采样,不致于加载过大图片出现内存溢出

* @param bytes

* @return

*/

public static Bitmap getBitmapByBytes(byte[] bytes){

//对于图片的二次采样,主要得到图片的宽与高

int width =0;

int height =0;

int sampleSize =1;//默认缩放为1

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

options.inJustDecodeBounds =true;//仅仅解码边缘区域

//如果指定了inJustDecodeBounds,decodeByteArray将返回为空

BitmapFactory.decodeByteArray(bytes,0, bytes.length, options);

//得到宽与高

height = options.outHeight;

width = options.outWidth;

//图片实际的宽与高,根据默认最大大小值,得到图片实际的缩放比例

while((height / sampleSize > Cache.IMAGE_MAX_HEIGHT)|| (width / sampleSize > Cache.IMAGE_MAX_WIDTH)) {

sampleSize *=2;

}

//不再只加载图片实际边缘

options.inJustDecodeBounds =false;

//并且制定缩放比例

options.inSampleSize = sampleSize;

return BitmapFactory.decodeByteArray(bytes,0, bytes.length, options);

}

//默认大小

class Cache{

public static final int IMAGE_MAX_HEIGH=854;

public static final int IMAGE_MAX_WIDTH=480;

}

二、图像质量压缩

private Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos =newByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG,100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options =100;

while( baos.toByteArray().length /1024>100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中

options -=10;//每次都减少10

}

ByteArrayInputStream isBm =new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中

Bitmap bitmap = BitmapFactory.decodeStream(isBm,null,null);//把ByteArrayInputStream数据生成图片

return bitmap;

}

三、图片按比例大小压缩(根据路径获取图片并压缩)

private Bitmap getimage(String srcPath) {

BitmapFactory.Options newOpts =newBitmapFactory.Options();

//开始读入图片,此时把options.inJustDecodeBounds 设回true了

newOpts.inJustDecodeBounds =true;

Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空

newOpts.inJustDecodeBounds =false;

int w = newOpts.outWidth;

int h = newOpts.outHeight;

//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为

float hh = 800f;//这里设置高度为800f

float ww = 480f;//这里设置宽度为480f

//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

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

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

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

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

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

}

if(be <=0)

be =1;

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

//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

return compressImage(bitmap);//压缩好比例大小后再进行质量压缩

}

四、图片按比例大小压缩方法(根据Bitmap图片压缩):

private Bitmap comp(Bitmap image) {

ByteArrayOutputStream baos =newByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG,100, baos);

if( baos.toByteArray().length /1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG,50, baos);//这里压缩50%,把压缩后的数据存放到baos中

}

ByteArrayInputStream isBm =newByteArrayInputStream(baos.toByteArray());

BitmapFactory.Options newOpts =newBitmapFactory.Options();

//开始读入图片,此时把options.inJustDecodeBounds 设回true了

newOpts.inJustDecodeBounds =true;

Bitmap bitmap = BitmapFactory.decodeStream(isBm,null, newOpts);

newOpts.inJustDecodeBounds =false;

int w = newOpts.outWidth;

int h = newOpts.outHeight;

//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为

float hh = 800f;//这里设置高度为800f

float ww = 480f;//这里设置宽度为480f

//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

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

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

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

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

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

}

if(be <=0)

be =1;

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

//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

isBm =newByteArrayInputStream(baos.toByteArray());

bitmap = BitmapFactory.decodeStream(isBm,null, newOpts);

return compressImage(bitmap);//压缩好比例大小后再进行质量压缩

}

相关文章

  • 图片压缩

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

  • Android 图片压缩

    参考 Android 图片压缩 此篇文章不讨论质量压缩,比例压缩与采样率压缩,只考虑使用libjpeg,绕过and...

  • Android性能优化--图片压缩

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

  • Android中RGB_565方式压缩图片详解

    Android压缩图片的方式 质量压缩 采样率压缩 缩放压缩 色彩压缩 RGB_565简介 RGB_565是色彩压...

  • Android 图片二次采样、质量压缩

    引言: Android系统支持几种图片(.png (preferred), .jpg (acceptable), ...

  • 关于android 图片压缩你需要知道的事

    关键词: Bitmap,质量压缩,比例压缩,采样率压缩,微信分享 前言 android 系统的图片压缩大体上有三种...

  • 图片压缩框架LuBan源码解析

    首先科普下图片压缩的基础知识在 Android 平台上,默认提供的压缩有三种方式:质量压缩和两种尺寸压缩,邻近采样...

  • 图片优化(一)Bitmap优化

    1.BitmapFactory2.图片存在的形式3.图片压缩方式 质量压缩 尺寸压缩 采样率压缩 1.图片工具类:...

  • 图片压缩

    图片压缩质量压缩,比例压缩,采样率压缩,JPEG压缩思路:根据控件的尺寸或图片要放大显示的尺寸作为参数去压缩图片原...

  • Android图片压缩(二次采样)

    一、简介: 在开发过程中,我们或多或少的都会接触到Bitmap这个东西,用的不好的话就会出现OOM问题,同时,也会...

网友评论

      本文标题:Android 图片二次采样、质量压缩

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