Android第三方开源库:图片压缩

来源:互联网 发布:北京同德软件 编辑:程序博客网 时间:2024/06/05 03:45

CompressHelper

github:https://github.com/nanchen2251/CompressHelper

原图:
这里写图片描述

权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

gradle

allprojects {    repositories {        maven { url 'https://jitpack.io' }        jcenter()    }}dependencies {    ...   compile 'com.github.nanchen2251:CompressHelper:1.0.5'}

默认压缩

File newFile = compressHelper.compressToFile(oldFile);

自定义压缩

File file = new CompressHelper.Builder(MainActivity.this)        .setMaxWidth(500)        .setMaxHeight(300)        .setFileName("newName")//自动添加后缀名.jpeg        .setQuality(50)        .setDestinationDirectoryPath(Environment.getExternalStorageDirectory().getAbsolutePath())//压缩后的文件保存路径        .build()        .compressToFile(oldFile);

问题

app module 的targetSdkVersion 22以下,25压缩后失败,注释掉

这个有时候报错,有时候不报错

setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath())

就可以正常压缩了。

双后缀名的问题

这里写图片描述
在压缩后,出现了2个后缀名,排查发现

.setFileName(oldFile.getName()).setCompressFormat(Bitmap.CompressFormat.JPEG)

造成的,file.getName()获取的是文件名是包含后缀名的,而我们又自定义了.JPEG的后缀名,导致双后缀名。

解决方法:
方法一:
不自定义压缩,使用默认的压缩,这样只有1个后缀名

File newFile = CompressHelper.getDefault(context).compressToFile(oldFile);

方法二:
给压缩文件命名是,不使用原文件名,或去掉原文件名的后缀名。

int index = oldFile.getName().indexOf(".");String newFileName = oldFile.getName().substring(0, index);

其他

Demo:http://git.oschina.net/libraryDemo/compresshelper01

ImageCompressUtil

功能这么齐全的图片压缩类,还有谁?

TakePhoto

功能全面,有多图选择+图片裁剪+图片压缩。压缩如果设置最大值的话,采用的而是循环压缩。
github :https://github.com/crazycodeboy/TakePhoto

gradle

compile 'com.jph.takephoto:takephoto_library:4.0.3'

配置

CropOptions是用于裁剪的配置类
CompressConfig是用于压缩的配置类

第一种:继承的方式

  1. 继承TakePhotoActivity、TakePhotoFragmentActivity、TakePhotoFragment三者之一。
  2. 通过getTakePhoto()获取TakePhoto实例进行相关操作。
  3. 重写以下方法获取结果
void takeSuccess(TResult result);void takeFail(TResult result,String msg);void takeCancel();

第二种:组装的方式

见README.md

简单使用

打开图片选择器

在Button的点击事件中,添加下面代码

TakePhoto takePhoto = getTakePhoto();takePhoto.onEnableCompress(TakePhotoHelper.getCompressConfig(), true);takePhoto.onPickMultipleWithCrop(6, TakePhotoHelper.getCropOptions());//        takePhoto.onPickMultiple(3);//不剪切

TakePhotoHelper是对剪切和压缩配置进行工具封装

public class TakePhotoHelper {    private static CompressConfig config;    private static CropOptions cropOptions;    public static CompressConfig getCompressConfig() {        if (config == null) {            config = new CompressConfig.Builder()                    .setMaxSize(102400)//压缩到的最大大小,单位B,采用的是循环压缩,调用enableQualityCompress(true)                    .setMaxPixel(800)//长或宽不超过的最大像素,单位px                    .enableReserveRaw(true)//是否保留原文件//                    .enableQualityCompress(true)//是否启动质量压缩                    .enablePixelCompress(true)//是否启用像素压缩                    .create();        }        return config;    }    public static CropOptions getCropOptions() {        if (cropOptions == null) {            cropOptions = new CropOptions.Builder()                    .setAspectX(800)                    .setAspectY(800)                    .setWithOwnCrop(true)//是否剪切                    .create();        }        return cropOptions;    }}

重写3个方法

@Overridepublic void takeFail(TResult result, String msg) {    super.takeFail(result, msg);}@Overridepublic void takeCancel() {    super.takeCancel();}@Overridepublic void takeSuccess(TResult result) {    super.takeSuccess(result);    ArrayList<TImage> images = result.getImages();    Map<String, File> map = new HashMap<>();    for (int i = 0; i < images.size(); i++) {        File file = new File(images.get(i).getCompressPath());        map.put(file.getName(), file);        Log.d(TAG, "file.getName()=" + file.getName());    }    Log.d(TAG, "文件数量=" + map.size());}

其中

images.get(i).getCompressPath()//压缩图片路径images.get(i).getOriginalPath()//原始图片路径
0 0
原创粉丝点击