首页 > 系统 > Android > 正文

Android图片压缩总结

2019-11-09 16:51:29
字体:
来源:转载
供稿:网友

通常开发中会对一张图片进行压缩,通常有三种方式:质量压缩,尺寸压缩,采样率压缩。

一.质量压缩通常通过设置bitmap的options属性来降低图片的质量,但图片的像素不会减少,所占内存也不会减少

public static void comPRessImageToFile(Bitmap bmp,File file) {//bmp需要压缩的bitmap图片对象 file压缩后图片保存的位置	        // 0-100 100为不压缩	        int options = 20;	        ByteArrayOutputStream baos = new ByteArrayOutputStream();	        // 把压缩后的数据存放到baos中	        bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);	        try {	            FileOutputStream fos = new FileOutputStream(file);	            fos.write(baos.toByteArray());	            fos.flush();	            fos.close();	        } catch (Exception e) {	            e.printStackTrace();	        }	    }

二.尺寸压缩,通过缩放图片像素减少占用内存大小

public static void compressBitmapToFile(Bitmap bmp, File file){// 尺寸压缩倍数,值越大,图片尺寸越小int ratio = 8;// 压缩Bitmap到对应尺寸Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888);Canvas canvas = new Canvas(result);Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);canvas.drawBitmap(bmp, null, rect, null);ByteArrayOutputStream baos = new ByteArrayOutputStream();// 把压缩后的数据存放到baos中result.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);try {FileOutputStream fos = new FileOutputStream(file);fos.write(baos.toByteArray());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}

三.采样率压缩

 /**	     * 设置图片的采样率,降低图片像素	     * @param filePath	     * @param file	     */	    public static void compressBitmap(String filePath, File file){	        // 数值越高,图片像素越低	        int inSampleSize = 8;	        BitmapFactory.Options options = new BitmapFactory.Options();	        options.inJustDecodeBounds = false;//	        options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。	        //采样率	        options.inSampleSize = inSampleSize;	        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);	        ByteArrayOutputStream baos = new ByteArrayOutputStream();	        // 把压缩后的数据存放到baos中	        bitmap.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);	        try {	            if(file.exists())	            {	                file.delete();	            }	            else {	                file.createNewFile();	            }	            FileOutputStream fos = new FileOutputStream(file);	            fos.write(baos.toByteArray());	            fos.flush();	            fos.close();	        } catch (Exception e) {	            e.printStackTrace();	        }	    }

附加:工具类 计算缩放比:

/**	     * 计算缩放比	     *	     * @param bitWidth  当前图片宽度	     * @param bitHeight 当前图片高度	     * @return	     * @Description:函数描述	     */	    public static int getRatioSize(int bitWidth, int bitHeight) {	        // 图片最大分辨率	        int imageHeight = 1920;	        int imageWidth = 1080;	        // 缩放比	        int ratio = 1;	        // 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可	        if (bitWidth > bitHeight && bitWidth > imageWidth) {	            // 如果图片宽度比高度大,以宽度为基准	            ratio = bitWidth / imageHeight;	        } else if (bitWidth < bitHeight && bitHeight > imageHeight) {	            // 如果图片高度比宽度大,以高度为基准	            ratio = bitHeight / imageHeight;	        }	        // 最小比率为1	        if (ratio <= 0)	            ratio = 1;	        return ratio;	    }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表