码迷,mamicode.com
首页 > 移动开发 > 详细

Android ---------压缩图片尺寸和大小

时间:2016-07-08 21:29:03      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

    package com.funs.compressImg.utils;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    
    /**图片压缩帮助类,有三种不同压缩图片的方法:
     * 1、质量压缩方法
     * 2、根据路径按比例大小压缩
     * 3、根据Bitmap按比例大小压缩
     */
    public class ImageCompressUtil {
    
        /**
         * 质量压缩法
         * @param image        压缩的资源图片(Bitmap)
         * @param aimSize    图片压缩的目标大小,单位为KB
         */
        public static Bitmap compressImage(Bitmap image, int aimSize){
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
            
            int options = 100;//定义压缩比例,初始为不压缩
            
            while((baos.toByteArray().length/1024) > aimSize){//循环判断压缩后图片是否大于目的大小,若大于则继续压缩
                
                baos.reset();//重置baos,即清空baos
                
                image.compress(Bitmap.CompressFormat.JPEG, options, baos);//将图片压缩options%,把压缩后的数据存放到baos中
                
                options -= 10;//options每次减少10
            }
            
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
            
            Bitmap bitmap = BitmapFactory.decodeStream(bais, null, null);//把ByteArrayInputStream数据生成图片
            return bitmap;
        }
        
        /**
         * 按比例缩放资源图片的宽高,然后再按质量压缩。注意:图片只能缩小,不能放大
         * @param imgPath        资源图片路径
         * @param aimWidth        将图片缩小到的期望宽度,单位为px
         * @param aimHeight        将图片缩小到的期望高度,单位为px
         * @param aimSize        图片压缩的目标大小,单位为KB
         */
        public static Bitmap getImage(String imgPath, float aimWidth, float aimHeight,int aimSize){
            BitmapFactory.Options opts = new BitmapFactory.Options();
            //开始读入图片,此时把options.inJustDecodeBounds设置为true,只得到宽高等参数,不传递真实数据
            opts.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath, opts);//此时返回的Bitmap为空
            
            opts.inJustDecodeBounds = false;
            int width = opts.outWidth;
            int height = opts.outHeight;
            //缩放比,由于是固定比例缩小,只用高或宽其中一个数据进行计算即可,值为1表示不缩小
            int ratio = 1;
            if (width > height && width > aimWidth) {
                ratio = (int) (width/aimWidth);
            }else if (height > width && height > aimHeight) {
                ratio = (int) (height/aimHeight);
            }
            if (ratio <= 0) {
                ratio = 1;
            }
            //设置缩小比例
            opts.inSampleSize = ratio;
            //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
            bitmap = BitmapFactory.decodeFile(imgPath, opts);
            return compressImage(bitmap, aimSize);
        }
        
        /**
         * 按比例缩放资源图片的宽高,然后再按质量压缩。注意:图片只能缩小,不能放大
         * @param image            资源图片(Bitmap)
         * @param aimWidth        将图片缩小到的期望宽度,单位为px
         * @param aimHeight        将图片缩小到的期望高度,单位为px
         * @param aimSize        图片压缩的目标大小,单位为KB
         */
        public static Bitmap comp(Bitmap image, float aimWidth, float aimHeight,int aimSize){
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            if ((baos.toByteArray().length/1024) > 1024) {//判断图片是否大于1M,如果是则进行压缩,避免在生成图片()
                baos.reset();//重置baos,即清空baos。
                image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            BitmapFactory.Options opts = new BitmapFactory.Options();
            //开始读入图片,此时把options.inJustDecodeBounds设回true了
            opts.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeStream(bais, null, opts);
            opts.inJustDecodeBounds = false;
            int width = opts.outWidth;
            int height = opts.outHeight;
            //缩放比,由于是固定比例缩小,只用高或宽其中一个数据进行计算即可,1表示不缩小
            int ratio = 1;
            if (width > height && width > aimWidth) {
                ratio = (int) (width/aimWidth);
            }else if(height > width && height > aimHeight){
                ratio = (int) (height/aimHeight);
            }
            if (ratio <= 0) {
                ratio = 1;
            }
            opts.inSampleSize = ratio;//设置缩小比例
            //重新读入图片,注意此时已经把options.inJustDecodeBounds设回false了
            bais = new ByteArrayInputStream(baos.toByteArray());
            bitmap = BitmapFactory.decodeStream(bais, null, opts);
            return compressImage(bitmap, aimSize);
        }
    }

Android ---------压缩图片尺寸和大小

标签:

原文地址:http://www.cnblogs.com/tittles0k/p/5654474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!