Skip to content

Commit

Permalink
solve issues like #187
Browse files Browse the repository at this point in the history
  • Loading branch information
郑梓斌 committed May 18, 2018
1 parent 23ef40a commit 555eb9d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 34 deletions.
34 changes: 27 additions & 7 deletions example/src/main/java/top/zibin/luban/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -27,6 +31,7 @@
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;
import top.zibin.luban.OnRenameListener;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "Luban";
Expand Down Expand Up @@ -75,8 +80,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mImageList.clear();

ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
// compressWithLs(photos);
compressWithRx(photos);
compressWithLs(photos);
// compressWithRx(photos);
}
}
}
Expand All @@ -87,14 +92,17 @@ private void compressWithRx(final List<String> photos) {
.map(new Function<List<String>, List<File>>() {
@Override
public List<File> apply(@NonNull List<String> list) throws Exception {
return Luban.with(MainActivity.this).load(list).get();
return Luban.with(MainActivity.this)
.load(list)
.get();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<File>>() {
@Override
public void accept(@NonNull List<File> list) throws Exception {
for (File file : list) {
Log.i(TAG, file.getAbsolutePath());
showResult(photos, file);
}
}
Expand All @@ -115,19 +123,31 @@ public boolean apply(String path) {
return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
}
})
.setCompressListener(new OnCompressListener() {
.setRenameListener(new OnRenameListener() {
@Override
public void onStart() {
public String rename(String filePath) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(filePath.getBytes());
return new BigInteger(1, md.digest()).toString(32);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
})
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() { }

@Override
public void onSuccess(File file) {
Log.i(TAG, file.getAbsolutePath());
showResult(photos, file);
}

@Override
public void onError(Throwable e) {
}
public void onError(Throwable e) { }
}).launch();
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/top/zibin/luban/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ String extSuffix(String path) {
return path.substring(path.lastIndexOf("."), path.length());
}

boolean isNeedCompress(int leastCompressSize, String path) {
boolean needCompress(int leastCompressSize, String path) {
if (leastCompressSize > 0) {
File source = new File(path);
return source.exists() && source.length() > (leastCompressSize << 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

/**
* 通过此接口获取输入流,以兼容文件、FileProvider方式获取到的图片
* <p>
* Get the input stream through this interface, and obtain the picture using compatible files and FileProvider
* Created by MrFeng on 2018/4/23.
*/
public interface InputStreamProvider {

InputStream open() throws IOException;

String getPath();
Expand Down
72 changes: 47 additions & 25 deletions library/src/main/java/top/zibin/luban/Luban.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Luban implements Handler.Callback {

private String mTargetDir;
private int mLeastCompressSize;
private OnRenameListener mRenameListener;
private OnCompressListener mCompressListener;
private CompressionPredicate mCompressionPredicate;
private List<InputStreamProvider> mStreamProviders;
Expand All @@ -39,6 +40,7 @@ public class Luban implements Handler.Callback {

private Luban(Builder builder) {
this.mTargetDir = builder.mTargetDir;
this.mRenameListener = builder.mRenameListener;
this.mStreamProviders = builder.mStreamProviders;
this.mCompressListener = builder.mCompressListener;
this.mLeastCompressSize = builder.mLeastCompressSize;
Expand All @@ -51,7 +53,7 @@ public static Builder with(Context context) {
}

/**
* Returns a mFile with a cache audio name in the private cache directory.
* Returns a file with a cache image name in the private cache directory.
*
* @param context A context.
*/
Expand All @@ -68,6 +70,16 @@ private File getImageCacheFile(Context context, String suffix) {
return new File(cacheBuilder);
}

private File getImageCustomFile(Context context, String filename) {
if (TextUtils.isEmpty(mTargetDir)) {
mTargetDir = getImageCacheDir(context).getAbsolutePath();
}

String cacheBuilder = mTargetDir + "/" + filename;

return new File(cacheBuilder);
}

/**
* Returns a directory with a default name in the private cache directory of the application to
* use to store retrieved audio.
Expand Down Expand Up @@ -125,20 +137,7 @@ public void run() {
try {
mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_START));

File result;
if (mCompressionPredicate != null) {
if (mCompressionPredicate.apply(path.getPath())) {
result = Checker.SINGLE.isNeedCompress(mLeastCompressSize, path.getPath()) ?
new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress() :
new File(path.getPath());
} else {
result = new File(path.getPath());
}
} else {
result = Checker.SINGLE.isNeedCompress(mLeastCompressSize, path.getPath()) ?
new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress() :
new File(path.getPath());
}
File result = compress(context, path);

mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_SUCCESS, result));
} catch (IOException e) {
Expand All @@ -165,20 +164,37 @@ private List<File> get(Context context) throws IOException {
Iterator<InputStreamProvider> iterator = mStreamProviders.iterator();

while (iterator.hasNext()) {
InputStreamProvider path = iterator.next();
results.add(compress(context, iterator.next()));
iterator.remove();
}

return results;
}

if (mCompressionPredicate != null) {
results.add(mCompressionPredicate.apply(path.getPath()) ?
new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress() :
new File(path.getPath()));
private File compress(Context context, InputStreamProvider path) throws IOException {
File result;

File outFile = getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()));

if (mRenameListener != null) {
String filename = mRenameListener.rename(path.getPath());
outFile = getImageCustomFile(context, filename);
}

if (mCompressionPredicate != null) {
if (mCompressionPredicate.apply(path.getPath())
&& Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath())) {
result = new Engine(path, outFile).compress();
} else {
results.add(new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress());
result = new File(path.getPath());
}

iterator.remove();
} else {
result = Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath()) ?
new Engine(path, outFile).compress() :
new File(path.getPath());
}

return results;
return result;
}

@Override
Expand All @@ -202,10 +218,11 @@ public boolean handleMessage(Message msg) {
public static class Builder {
private Context context;
private String mTargetDir;
private List<InputStreamProvider> mStreamProviders;
private int mLeastCompressSize = 100;
private OnRenameListener mRenameListener;
private OnCompressListener mCompressListener;
private CompressionPredicate mCompressionPredicate;
private List<InputStreamProvider> mStreamProviders;

Builder(Context context) {
this.context = context;
Expand Down Expand Up @@ -277,6 +294,11 @@ public Builder putGear(int gear) {
return this;
}

public Builder setRenameListener(OnRenameListener listener) {
this.mRenameListener = listener;
return this;
}

public Builder setCompressListener(OnCompressListener listener) {
this.mCompressListener = listener;
return this;
Expand Down
22 changes: 22 additions & 0 deletions library/src/main/java/top/zibin/luban/OnRenameListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package top.zibin.luban;

/**
* Author: zibin
* Datetime: 2018/5/18
* <p>
* 提供修改压缩图片命名接口
* <p>
* A functional interface (callback) that used to rename the file after compress.
*/
public interface OnRenameListener {

/**
* 压缩前调用该方法用于修改压缩后文件名
* <p>
* Call before compression begins.
*
* @param filePath 传入文件路径/ file path
* @return 返回重命名后的字符串/ file name
*/
String rename(String filePath);
}

0 comments on commit 555eb9d

Please sign in to comment.