在你的项目的根目录的build.gradle中添加如下信息:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
然后在添加相关的依赖关系(不是根目录)
dependencies {
implementation 'com.github.heynchy:CompressImage:v0.1.2'
}
注意:对相关图片进行压缩时要确保手机的存储权限已经获得
// TODO 进行质量压缩
/**
* 确保该方法执行在主线程中
*/
runOnUiThread(new Runnable() {
@Override
public void run() {
/**
* 参数解析:
* filePath: 要压缩图片的绝对路径
* savePath: 压缩图片后的保存路径
* maxSize: (例如1024)期望压缩的图片<= maxsize;单位为 KB
*/
CompressImage.getInstance().imageMassCompress(filePath, savePath, 1024,
new CompressMassListener() {
@Override
public void onCompressMassSuccessed(final String imgPath) {
// 返回值: imgPath----压缩后图片的绝对路径
runOnUiThread(new Runnable() {
@Override
public void run() {
/**
* 执行相关的UI操作确保在主线程中进行
*/
Bitmap bitmap = PermissionUtil.getLoacalBitmap(imgPath);
mImagePressIv.setImageBitmap(bitmap);
mMassPressTv.setText("压缩后质量: " + FileSizeUtil.getFileOrFilesSize(imgPath));
}
});
}
@Override
public void onCompressMassFailed(final String imgPath, final String msg) {
/**
* 返回值: imgPath----原图片的绝对路径
* msg----返回的错误信息
*/
runOnUiThread(new Runnable() {
@Override
public void run() {
/**
* 执行相关的UI操作确保在主线程中进行
*/
Toast.makeText(MassImageActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
}
});
}
});
// TODO 进行像素压缩
/**
* 参数解析:
* filePath: 要压缩图片的绝对路径
* savePath: 压缩图片后的保存路径
* maxWidth: 期望压缩后图片的宽度(像素值)
* maxHeight: 期望压缩后图片的高度(像素值)
*/
CompressImage.getInstance().imagePixCompress(filePath, savePath, 100, 100,
new CompressPixListener() {
@Override
public void onCompressPixSuccessed(String imgPath, Bitmap bitmap) {
/**
* 返回值: imgPath----压缩后图片的绝对路径
* bitmap----返回的图片
*/
mImagePressIv.setImageBitmap(bitmap);
mMassPressTv.setText("压缩后质量: " + FileSizeUtil.getFileOrFilesSize(imgPath));
}
@Override
public void onCompressPixFailed(String imgPath, String msg) {
/**
* 返回值: imgPath----原图片的绝对路径
* msg----返回的错误信息
*/
Toast.makeText(PixImageActivity.this, "压缩失败", Toast.LENGTH_SHORT).show();
}
});
// TODO 进行Luban算法压缩
/**
* 参数解析:
* filePath: 要压缩图片的绝对路径
* savePath: 压缩图片后的保存路径
*/
CompressImage.getInstance().imageLubrnCompress(filePath, savePath, new CompressLubanListener() {
@Override
public void onCompressLubanSuccessed(String imgPath, Bitmap bitmap) {
/**
* 返回值: imgPath----压缩后图片的绝对路径
* bitmap----返回的图片
*/
mImagePressIv.setImageBitmap(bitmap);
mMassPressTv.setText("压缩后质量: " + FileSizeUtil.getFileOrFilesSize(imgPath));
}
@Override
public void onCompressLubanFailed(String imgPath, String msg) {
/**
* 返回值: imgPath----原图片的绝对路径
* msg----返回的错误信息
*/
Toast.makeText(LubanImageActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
感谢以下相关项目: https://github.com/Curzibn/Luban
Copyright 2017 heynchy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.