Android应用开发之Android 图片压缩(防止OOM、尺寸压缩、质量压缩)
凌雪 2018-10-24 来源 :网络 阅读 1338 评论 0

摘要:本文将带你了解Android应用开发之Android 图片压缩(防止OOM、尺寸压缩、质量压缩),希望本文对大家学Android有所帮助。

本文将带你了解Android应用开发之Android 图片压缩(防止OOM、尺寸压缩、质量压缩),希望本文对大家学Android有所帮助。


Android开发中,图片的处理基本有有所接触,对于图片的压缩也有很多的博客提到,我也是查看了许多的博客后,自己总结了一下,写了一个测试的Demo,我测试的图片是6720*4480 12.9M的图片,压缩到91.24KB,下面是全部的代码,有需要改进的地方请在评论区提出,Android新人还请各位大牛多多指教 !
首先在加载图片的时候要防止OOM
/** * 将本地路径转化为Bitmap * * @param path 本地路径 * @return 返回本地资源的Bitmap */private Bitmap loadFile(String path)   {    File mFile = new   File(path);    Bitmap mBitmap =   null;    //判读文件是否存在    if (mFile.exists())   {        BitmapFactory.Options   mOptions = new   BitmapFactory.Options();        //设置只加载尺寸资源        mOptions.inJustDecodeBounds   = true;        //设置资源        BitmapFactory.decodeFile(path,   mOptions);        //图片加载缩方处理        coundScale(mOptions);        //设置加载资源模式        mOptions.inJustDecodeBounds   = false;        //设置资源        mBitmap   = BitmapFactory.decodeFile(path,   mOptions);    }    return   mBitmap;}  /** * 计算缩放比例,防止OOM * * @param opeions   BitmapFactory.Options */private void coundScale(BitmapFactory.Options   opeions) {    if (opeions != null)   {        //原图的尺寸        int   orginalWidht =   opeions.outWidth;        int   orginalHieght =   opeions.outHeight;        //如果图片尺寸大于一定尺寸的时候进行缩放        if   (orginalWidht > 1080 || orginalHieght > 1920)   {            //缩小倍数            int   scale =   1;             if   (orginalWidht > orginalHieght)   {                //长图片的时候(以宽度为基准缩小)                scale   = orginalWidht /   1080;            }   else if (orginalWidht < orginalHieght) {                //高图的时候(以高度为基准缩小)                scale   = orginalHieght /   1920;            }   else {                //正方形图的时候(以宽度为基准缩小)                scale   = orginalWidht /   1080;            }            Log.i(TAG,   "coundScale: " +   scale);            opeions.inSampleSize   =   scale;        }    }}
统一图片的尺寸
/** * 进行图片尺寸裁剪 * * @param loadBitmap 本地图片资源 * @return 尺寸处理后的资源 */private Bitmap cutFile(Bitmap loadBitmap)   {    Bitmap mCutBitmap = null;    if   (loadBitmap != null) {        //获取经过第一次处理后图片的尺寸;        int   mLoadBitmapWidth =   loadBitmap.getWidth();        int   mLoadBitmapHeight =   loadBitmap.getHeight();        float   scaleX = 0;        float scaleY =   0;        //图片的宽高比例        float picScale   = (float) mLoadBitmapWidth /   mLoadBitmapHeight;        //计算压缩尺寸比(无论图片大小,直接规定尺寸)        if   (mLoadBitmapWidth > mLoadBitmapHeight)   {            //长图片的时候(宽度缩放到1080,高度安装原图的宽高比缩放尺寸)            scaleX   = 1080.0F /   mLoadBitmapWidth;            scaleY   = 1080.0F / picScale / mLoadBitmapHeight;        }   else if (mLoadBitmapWidth < mLoadBitmapHeight)   {            //高图的时候            scaleX   = 1080.0F /   mLoadBitmapWidth;            scaleY   = 1920.0F / mLoadBitmapHeight;        }   else   {            //正方形图的时候(按照宽度为基准缩放)            scaleX   = 1080.0F /   mLoadBitmapWidth;            scaleY   = scaleX;        }        //创建一个矩阵        Matrix   mMatrix = new Matrix();        //设置缩放比例        mMatrix.postScale(scaleX,   scaleY);        //生成新的Bitmap        mCutBitmap   = Bitmap.createBitmap(loadBitmap, 0, 0, mLoadBitmapWidth, mLoadBitmapHeight,   mMatrix, false);    }    return   mCutBitmap;}
降低图片的质量
/** * 质量压缩 * * @param mBitmap 处理尺寸后的资源 * @return 压缩结束后的文件 */private File qualtiy(Bitmap mBitmap)   {    File mFile = null;    if   (mBitmap != null) {        //设置压缩后图片不超过100KB        int   maxKB = 100;        //创建一个缓存区        ByteArrayOutputStream   mByteArrayOutputStream = new   ByteArrayOutputStream();        //默认压缩质量(不压缩)        int options =   100;        //压缩后的大小        int   endKB = 0;        do   {            //清空缓存区            mByteArrayOutputStream.reset();            //将数据写入缓存区            mBitmap.compress(Bitmap.CompressFormat.JPEG,   options,   mByteArrayOutputStream);            //降低质量            options   -=   5;            //重新计算缓存区数据大小            endKB   =   mByteArrayOutputStream.toByteArray().length;            //如果压缩系数小于0的时候直接跳出循环            if   (options <= 0)   {                break;            }            //判断当前资源大小是否大于期望大小        }   while (endKB / 1024 >   maxKB);        //创建一个输入流        FileOutputStream   mFileOutputStream =   null;        BufferedOutputStream   mBufferedOutputStream =   null;        //创建一个空文件        mFile   = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +   "/manageEnd.jpg");        try   {            mFileOutputStream   = new   FileOutputStream(mFile);            mBufferedOutputStream   = new   BufferedOutputStream(mFileOutputStream);            //将压缩后的数据写入都空文件中            mBufferedOutputStream.write(mByteArrayOutputStream.toByteArray());        }   catch (FileNotFoundException e) {            e.printStackTrace();        }   catch (IOException e)   {            e.printStackTrace();        }   finally   {            try   {                if   (mByteArrayOutputStream != null && mBufferedOutputStream != null   && mFileOutputStream != null)   {                    //关闭流                    mByteArrayOutputStream.flush();                    mBufferedOutputStream.flush();                    mFileOutputStream.close();                    mBufferedOutputStream.close();                    mByteArrayOutputStream.close();                }            }   catch (IOException e)   {                e.printStackTrace();            }        }    }    return   mFile;}    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved