android 和RxJava配合使用的两个图片压缩框架LuBan、Compressor

首先需要引入RxJava和RxAndroid

compile ‘io.reactivex.rxjava2:rxjava:2.1.7’

compile ‘io.reactivex.rxjava2:rxandroid:2.0.1’

/**

  • 使用Compressor RxJava模式压缩

  • @param path

*/

private void initCompressorRxJava(String path) {

new Compressor(this)

.compressToFileAsFlowable(new File(path))

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Consumer() {

@Override

public void accept(File file) {

Glide.with(CompressorActivity.this)

.load(file)

.into(mImageNew);

mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//170.27KB

}

}, new Consumer() {

@Override

public void accept(Throwable throwable) {

throwable.printStackTrace();

mText.setText(“压缩失败了”);

}

});

}

总结:

1、LuBan的优点:LuBan压缩方法在默认的情况下,压缩比要比Compressor要高,同时压缩的对象丰富,(包括file,path,List),可以传入多张图片集合直接进行压缩等。

2、Compressor的优点,返回的对象相对丰富,可以返回File或者BitMap,同时Compressor可以自定义压缩比例,定制化比较高等。

LuBan的gitHub地址:https://github.com/Curzibn/Luban

Compressor的gitHub地址:https://github.com/zetbaitsu/Compressor

整个测试源码:

package tsou.cn.lubancompressor;

import android.Manifest;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.graphics.Bitmap;

import android.os.Build;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.v4.app.ActivityCompat;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import id.zelory.compressor.Compressor;

import io.reactivex.Flowable;

import io.reactivex.android.schedulers.AndroidSchedulers;

import io.reactivex.functions.Consumer;

import io.reactivex.functions.Function;

import io.reactivex.schedulers.Schedulers;

import top.zibin.luban.Luban;

import top.zibin.luban.OnCompressListener;

public class CompressorActivity extends AppCompatActivity {

private ImageView mImageOld;

private ImageView mImageNew;

private static final String TYPE = “type”;

public static final int REQUEST_PICK_IMAGE = 11101;

String[] mPermissionList = new String[]{

Manifest.permission.WRITE_EXTERNAL_STORAGE,

Manifest.permission.READ_EXTERNAL_STORAGE};

private int type;

/**

  • LuBanCompressor

*/

private TextView mText;

public static void acti

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中进行图片压缩可以使用以下两种方式: 1. 使用 BitmapFactory 进行图片压缩 ```java public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // 首先将 inJustDecodeBounds 设置为 true,解析图片的宽高信息,不将图片的像素加载到内存中 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // 根据计算出的 inSampleSize 来进行图片压缩 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 将 inJustDecodeBounds 设置为 false,重新解析图片,这次会将图片的像素加载到内存中 options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // 获取图片的原始宽高 final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; // 如果图片的宽高比目标宽高大,则进行压缩 if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // 计算 inSampleSize 的值,确保宽高都不会超过目标宽高 while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; } ``` 调用该方法时,可以指定需要压缩到的目标宽高: ```java Bitmap compressedBitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.image, 500, 500); ``` 2. 使用第三方库进行图片压缩Android 中有很多第三方库可以用来进行图片压缩,比如: - Luban:https://github.com/Curzibn/Luban - Compressor:https://github.com/zetbaitsu/Compressor 这些库通常都提供了简单易用的 API,可以很方便地进行图片压缩

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值