-
Notifications
You must be signed in to change notification settings - Fork 192
/
Luban.java
286 lines (248 loc) · 8.63 KB
/
Luban.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*Copyright 2016 Zheng Zibin
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.*/
package me.shaohui.advancedluban;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.IntDef;
import android.util.Log;
import java.io.File;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
public class Luban {
public static final int FIRST_GEAR = 1;
public static final int THIRD_GEAR = 3;
public static final int CUSTOM_GEAR = 4;
private static final String TAG = "Luban";
private static String DEFAULT_DISK_CACHE_DIR = "luban_disk_cache";
private File mFile;
private List<File> mFileList;
private LubanBuilder mBuilder;
private Luban(File cacheDir) {
mBuilder = new LubanBuilder(cacheDir);
}
public static Luban compress(Context context, File file) {
Luban luban = new Luban(Luban.getPhotoCacheDir(context));
luban.mFile = file;
luban.mFileList = Collections.singletonList(file);
return luban;
}
public static Luban compress(Context context, List<File> files) {
Luban luban = new Luban(Luban.getPhotoCacheDir(context));
luban.mFileList = new ArrayList<>(files);
luban.mFile = files.get(0);
return luban;
}
/**
*
* @param file 要压缩的单个文件
* @param cacheDir 压缩完文件的存储路径
*/
public static Luban compress(File file, File cacheDir) {
if (!isCacheDirValid(cacheDir)) {
throw new IllegalArgumentException("The cacheDir must be Directory");
}
Luban luban = new Luban(cacheDir);
luban.mFile = file;
luban.mFileList = Collections.singletonList(file);
return luban;
}
public static Luban compress(List<File> files, File cacheDir) {
if (!isCacheDirValid(cacheDir)) {
throw new IllegalArgumentException("The cacheDir must be Directory");
}
Luban luban = new Luban(cacheDir);
luban.mFile = files.get(0);
luban.mFileList = new ArrayList<>(files);
return luban;
}
private static boolean isCacheDirValid(File cacheDir) {
return cacheDir.isDirectory() && (cacheDir.exists() || cacheDir.mkdirs());
}
/**
* 自定义压缩模式 FIRST_GEAR、THIRD_GEAR、CUSTOM_GEAR
*/
public Luban putGear(@GEAR int gear) {
mBuilder.gear = gear;
return this;
}
/**
* 自定义图片压缩格式
*/
public Luban setCompressFormat(Bitmap.CompressFormat compressFormat) {
mBuilder.compressFormat = compressFormat;
return this;
}
/**
* CUSTOM_GEAR 指定目标图片的最大体积
*/
public Luban setMaxSize(int size) {
mBuilder.maxSize = size;
return this;
}
/**
* CUSTOM_GEAR 指定目标图片的最大宽度
*
* @param width 最大宽度
*/
public Luban setMaxWidth(int width) {
mBuilder.maxWidth = width;
return this;
}
/**
* CUSTOM_GEAR 指定目标图片的最大高度
*
* @param height 最大高度
*/
public Luban setMaxHeight(int height) {
mBuilder.maxHeight = height;
return this;
}
/**
* listener调用方式,在主线程订阅并将返回结果通过 listener 通知调用方
*
* @param listener 接收回调结果
*/
public void launch(final OnCompressListener listener) {
asObservable().observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(
new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
listener.onStart();
}
})
.subscribe(new Consumer<File>() {
@Override
public void accept(File file) throws Exception {
listener.onSuccess(file);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
listener.onError(throwable);
}
});
}
/**
* listener调用方式,在主线程订阅并将返回结果通过 listener 通知调用方
*
* @param listener 接收回调结果
*/
public void launch(final OnMultiCompressListener listener) {
asListObservable().observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
listener.onStart();
}
})
.subscribe(new Consumer<List<File>>() {
@Override
public void accept(List<File> files) throws Exception {
listener.onSuccess(files);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
listener.onError(throwable);
}
});
}
/**
* 返回File Observable
*/
public Observable<File> asObservable() {
LubanCompresser compresser = new LubanCompresser(mBuilder);
return compresser.singleAction(mFile);
}
/**
* 返回fileList Observable
*/
public Observable<List<File>> asListObservable() {
LubanCompresser compresser = new LubanCompresser(mBuilder);
return compresser.multiAction(mFileList);
}
// Utils
/**
* Returns a directory with a default name in the private cache directory of the application to
* use to store
* retrieved media and thumbnails.
*
* @param context A context.
* @see #getPhotoCacheDir(Context, String)
*/
private static File getPhotoCacheDir(Context context) {
return getPhotoCacheDir(context, Luban.DEFAULT_DISK_CACHE_DIR);
}
/**
* Returns a directory with the given name in the private cache directory of the application to
* use to store
* retrieved media and thumbnails.
*
* @param context A context.
* @param cacheName The name of the subdirectory in which to store the cache.
* @see #getPhotoCacheDir(Context)
*/
private static File getPhotoCacheDir(Context context, String cacheName) {
File cacheDir = context.getCacheDir();
if (cacheDir != null) {
File result = new File(cacheDir, cacheName);
if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) {
// File wasn't able to create a directory, or the result exists but not a directory
return null;
}
return result;
}
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "default disk cache dir is null");
}
return null;
}
/**
* 清空Luban所产生的缓存
* Clears the cache generated by Luban
*/
public Luban clearCache() {
if (mBuilder.cacheDir.exists()) {
deleteFile(mBuilder.cacheDir);
}
return this;
}
/**
* 清空目标文件或文件夹
* Empty the target file or folder
*/
private void deleteFile(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File file : fileOrDirectory.listFiles()) {
deleteFile(file);
}
}
fileOrDirectory.delete();
}
@IntDef({FIRST_GEAR, THIRD_GEAR, CUSTOM_GEAR})
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.SOURCE)
@Documented
@Inherited
@interface GEAR {
}
}