Android关于图片处理的一些方法

1.对图片进行质量压缩

    /**
     * 对图片进行质量压缩
     * @param bitmap 图片
     * @param size 压缩的大小
     * @return
     */
    public static Bitmap compressBitmap(Bitmap bitmap , int size) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int compress = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG,compress,outputStream);// 100表示不改变图片质量,把图片压缩后的数据保存到outputStream
        while(outputStream.toByteArray().length/1024 >size){//循环判断压缩后的图片是否大于压缩的大小
            outputStream.reset();
            compress -= 10; //一次减少10
            bitmap.compress(Bitmap.CompressFormat.JPEG,compress,outputStream);//继续保存压缩后的数据
        }
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());// 把压缩后的数据outputStream保存到ByteArrayInputStream
        Bitmap bitmap_new = BitmapFactory.decodeStream(inputStream);//生成新的图片
        return bitmap_new;
    }

2.对图片进行尺寸压缩

    /**
     * 图片按比例大小压缩方法
     * @param imagePath (根据路径获取图片并压缩)
     * @param width 压缩的宽度
     * @param height 压缩的高度
     */
    public static Bitmap compressBitmap(String imagePath , int width , int height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        
        int w = options.outWidth;
        int h = options.outHeight;
        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;// be=1表示不缩放
        if (w > h && w > width) {// 如果宽度大的话根据宽度固定大小缩放
            be = options.outWidth / width;
        } else if (w < h && h > height) {// 如果高度高的话根据宽度固定大小缩放
            be = options.outHeight / height;
        }
        if (be <= 0)
            be = 1;
        options.inSampleSize = be;// 设置缩放比例
        // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        options.inJustDecodeBounds = true;
        Bitmap bitmap  = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false;
        
        return bitmap;
    }


猜你喜欢

转载自blog.csdn.net/xuwenneng/article/details/78615217