关于图片的工具类

这篇博客记录了在Android项目中对图片进行处理的方法,包括图片压缩、转换为Base64字符串以及使用Picasso库加载图片。内容简洁,旨在提供基本操作的参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人项目中,对用到的图片的处理,全部放在一个工具类中了,这里mark一下

/**
 * 方法1  质量压缩
 *
 * @param beforBitmap
 * @return
 */
public static Bitmap compressImage(Bitmap beforBitmap) {

    // 可以捕获内存缓冲区的数据,转换成字节数组。
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    if (beforBitmap != null) {
        // 第一个参数:图片压缩的格式;第二个参数:压缩的比率;第三个参数:压缩的数据存放到bos        beforBitmap.    compress(Bitmap.CompressFormat.JPEG, 100, bos);
        int options = 100;
        // 循环判断压缩后的图片是否是大于100kb,如果大于,就继续压缩,否则就不压缩
        while (bos.toByteArray().length / 1024 > 100) {
            bos.reset();// 置为空
            // 压缩options%
            beforBitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);
            // 每次都减少10
            options -= 10;

        }
        // bos中将数据读出来 存放到ByteArrayInputStream        ByteArrayInputStream bis = new ByteArrayInputStream(
                bos.toByteArray());
        // 将数据转换成图片
        Bitmap afterBitmap = BitmapFactory.decodeStream(bis);
        return afterBitmap;
    }
    return null;
}
2.

/**
 * 图片压缩03
 *
 * @param filePath  要操作的图片的大小
 * @param newWidth  图片指定的宽度
 * @param newHeight 图片指定的高度
 * @return
 */
public static Bitmap compressBitmap(String filePath, double newWidth, double newHeight) {

    //根据路径 获得原图
    Bitmap beforeBitmap = BitmapFactory.decodeFile(filePath);

    // 图片原有的宽度和高度
    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;
}

3

/**
 * 通过base32  将图片处理为base64字符串
 *
 * @param bit
 * @return
 */
public static String Bitmap2StrByBase64(Bitmap bit) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bit.compress(Bitmap.CompressFormat.PNG, 40, bos);//参数100表示不压缩
    byte[] bytes = bos.toByteArray();
    return Base64.encodeToString(bytes, Base64.DEFAULT);
}

4.使用picasso加载图片

/**
 * 加载图片
 *
 * @param url 图片url
 * @param p   加载显示的控件
 */
public static void loadPic(String url, ImageView p, Activity ctx) {
    Picasso.with(ctx)
            .load(url)     //url
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(p);
    p.setVisibility(View.VISIBLE);
}

可以根据需求去改下代码,其实这些都是比较简单的,这里仅仅mark一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值