Several methods of Android image compression

Several methods of Android image compression
When the image is too large to meet the needs, the image needs to be cropped and compressed. There are three main compression processing interfaces commonly used: Quality compression method , size compression method, thumbnail compression method and equal proportion compression method, the specific codes are as follows:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ThumbnailUtils;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * 图片压缩的方法
 */
public class BitmapUtils {
    
    

    /**
     * 图片压缩:质量压缩方法
     * @param beforBitmap 要压缩的图片
     * @return 压缩后的图片
     */
    static private Bitmap compressImage(Bitmap beforeBitmap) {
    
    

        // 可以捕获内存缓冲区的数据,转换成字节数组。
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        if (beforeBitmap != null) {
    
    
            // 第一个参数:图片压缩的格式;第二个参数:压缩的比率;第三个参数:压缩的数据存放到bos中
            beforeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            
            // 循环判断压缩后的图片大小是否满足要求,这里限制100kb,若不满足则继续压缩,每次递减10%压缩
            int options = 100;
            while (bos.toByteArray().length / 1024 > 100) {
    
    
                bos.reset();// 置为空
                beforeBitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);
                options -= 10;
            }
            
            // 从bos中将数据读出来 转换成图片
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            Bitmap afterBitmap = BitmapFactory.decodeStream(bis);
            return afterBitmap;
        }
        return null;
    }

    /**
     * 图片压缩:获得缩略图
     * @param beforeBitmap 要压缩的图片
     * @param width 缩略图宽度
     * @param height 缩略图高度
     * @return 压缩后的图片
     */
    static public Bitmap getThumbnail(Bitmap beforeBitmap, int width, int height) {
    
    
        return ThumbnailUtils.extractThumbnail(beforeBitmap, width, height);
    }

    /**
     * 图片压缩: 按尺寸压缩
     * @param beforeBitmap 要压缩的图片
     * @param newWidth 压缩后的宽度
     * @param newHeight 压缩后的高度
     * @return 压缩后的图片
     */
    static public Bitmap compressBitmap1(Bitmap beforeBitmap, double newWidth, double newHeight) {
    
    
        // 图片原有的宽度和高度
        float beforeWidth = beforeBitmap.getWidth();
        float beforeHeight = beforeBitmap.getHeight();

        // 计算宽高缩放率
        float scaleWidth = 0;
        float scaleHeight = 0;
        if (beforeWidth > beforeHeight) {
    
    
            scaleWidth = ((float) newWidth) / beforeWidth;
            scaleHeight = ((float) newHeight) / beforeHeight;
        } else {
    
    
            scaleWidth = ((float) newWidth) / beforeHeight;
            scaleHeight = ((float) newHeight) / beforeWidth;
        }

        // 矩阵对象
        Matrix matrix = new Matrix();
        // 缩放图片动作 缩放比例
        matrix.postScale(scaleWidth, scaleHeight);
        // 创建一个新的Bitmap 从原始图像剪切图像
        Bitmap afterBitmap = Bitmap.createBitmap(beforeBitmap, 0, 0,
                (int) beforeWidth, (int) beforeHeight, matrix, true);
        return afterBitmap;
    }

    /**
     * 图片压缩: 规定尺寸等比例压缩,宽高不能超过限制要求
     * @param beforBitmap 要压缩的图片
     * @param maxWidth 最大宽度限制
     * @param maxHeight 最大高度限制
     * @return 压缩后的图片
     */
    static public Bitmap compressBitmap(Bitmap beforBitmap, double maxWidth, double maxHeight) {
    
    

        // 图片原有的宽度和高度
        float beforeWidth = beforBitmap.getWidth();
        float beforeHeight = beforBitmap.getHeight();
        if (beforeWidth <= maxWidth && beforeHeight <= maxHeight) {
    
    
            return beforBitmap;
        }

        // 计算宽高缩放率,等比例缩放
        float scaleWidth =  ((float) maxWidth) / beforeWidth;
        float scaleHeight = ((float)maxHeight) / beforeHeight;
        float scale = scaleWidth;
        if (scaleWidth > scaleHeight) {
    
    
            scale = scaleHeight;
        }
        Log.d("BitmapUtils", "before[" + beforeWidth + ", " + beforeHeight + "] max[" + maxWidth
                + ", " + maxHeight + "] scale:" + scale);

        // 矩阵对象
        Matrix matrix = new Matrix();
        // 缩放图片动作 缩放比例
        matrix.postScale(scale, scale);
        // 创建一个新的Bitmap 从原始图像剪切图像
        Bitmap afterBitmap = Bitmap.createBitmap(beforBitmap, 0, 0,
                (int) beforeWidth, (int) beforeHeight, matrix, true);
        return afterBitmap;
    }
}


Guess you like

Origin blog.csdn.net/weixin_38687303/article/details/125282450