-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
周晓东
committed
Jan 18, 2017
1 parent
478cf70
commit a0a64a5
Showing
4 changed files
with
131 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,96 +22,91 @@ | |
* Eamil:[email protected] | ||
*/ | ||
public class CompressWithLuBan implements CompressImage { | ||
private ArrayList<TImage> images; | ||
private CompressListener listener; | ||
private Context context; | ||
private LubanOptions options; | ||
private ArrayList<File> files = new ArrayList<>(); | ||
private ArrayList<TImage> images; | ||
private CompressListener listener; | ||
private Context context; | ||
private LubanOptions options; | ||
private ArrayList<File> files = new ArrayList<>(); | ||
|
||
public CompressWithLuBan(Context context, CompressConfig config, ArrayList<TImage> images, CompressListener listener) { | ||
options=config.getLubanOptions(); | ||
this.images = images; | ||
this.listener = listener; | ||
this.context = context; | ||
} | ||
public CompressWithLuBan(Context context, CompressConfig config, ArrayList<TImage> images, | ||
CompressListener listener) { | ||
options = config.getLubanOptions(); | ||
this.images = images; | ||
this.listener = listener; | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void compress() { | ||
if (images == null || images.isEmpty()) { | ||
listener.onCompressFailed(images, " images is null"); | ||
return; | ||
} | ||
for (TImage image : images) { | ||
if (image == null) { | ||
listener.onCompressFailed(images, " There are pictures of compress is null."); | ||
return; | ||
} | ||
files.add(new File(image.getOriginalPath())); | ||
} | ||
if (images.size() == 1) { | ||
compressOne(); | ||
} else { | ||
compressMulti(); | ||
} | ||
@Override public void compress() { | ||
if (images == null || images.isEmpty()) { | ||
listener.onCompressFailed(images, " images is null"); | ||
return; | ||
} | ||
for (TImage image : images) { | ||
if (image == null) { | ||
listener.onCompressFailed(images, " There are pictures of compress is null."); | ||
return; | ||
} | ||
files.add(new File(image.getOriginalPath())); | ||
} | ||
if (images.size() == 1) { | ||
compressOne(); | ||
} else { | ||
compressMulti(); | ||
} | ||
} | ||
|
||
private void compressOne() { | ||
Luban.get(context).putGear(options.getGear()) | ||
.load(files.get(0)) | ||
.setMaxHeight(options.getMaxHeight()) | ||
.setMaxWidth(options.getMaxWidth()) | ||
.setMaxSize(options.getMaxSize()/1000) | ||
.launch(new OnCompressListener() { | ||
@Override | ||
public void onStart() { | ||
private void compressOne() { | ||
Luban.compress(context, files.get(0)) | ||
.putGear(Luban.CUSTOM_GEAR) | ||
.setMaxHeight(options.getMaxHeight()) | ||
.setMaxWidth(options.getMaxWidth()) | ||
.setMaxSize(options.getMaxSize() / 1000) | ||
.launch(new OnCompressListener() { | ||
@Override public void onStart() { | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public void onSuccess(File file) { | ||
TImage image=images.get(0); | ||
image.setCompressPath(file.getPath()); | ||
image.setCompressed(true); | ||
listener.onCompressSuccess(images); | ||
} | ||
@Override public void onSuccess(File file) { | ||
TImage image = images.get(0); | ||
image.setCompressPath(file.getPath()); | ||
image.setCompressed(true); | ||
listener.onCompressSuccess(images); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
listener.onCompressFailed(images, e.getMessage() + " is compress failures"); | ||
} | ||
}); | ||
} | ||
@Override public void onError(Throwable e) { | ||
listener.onCompressFailed(images, e.getMessage() + " is compress failures"); | ||
} | ||
}); | ||
} | ||
|
||
private void compressMulti() { | ||
Luban.get(context).putGear(options.getGear()) | ||
.load(files) | ||
.setMaxSize(options.getMaxSize()/1000) // limit the final image size(unit:Kb) | ||
.setMaxHeight(options.getMaxHeight()) // limit image height | ||
.setMaxWidth(options.getMaxWidth()) | ||
.launch(new OnMultiCompressListener() { | ||
@Override | ||
public void onStart() { | ||
private void compressMulti() { | ||
Luban.compress(context, files) | ||
.putGear(Luban.CUSTOM_GEAR) | ||
.setMaxSize( | ||
options.getMaxSize() / 1000) // limit the final image size(unit:Kb) | ||
.setMaxHeight(options.getMaxHeight()) // limit image height | ||
.setMaxWidth(options.getMaxWidth()) | ||
.launch(new OnMultiCompressListener() { | ||
@Override public void onStart() { | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public void onSuccess(List<File> fileList) { | ||
handleCompressCallBack(fileList); | ||
} | ||
@Override public void onSuccess(List<File> fileList) { | ||
handleCompressCallBack(fileList); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
listener.onCompressFailed(images, e.getMessage() + " is compress failures"); | ||
} | ||
}); | ||
} | ||
@Override public void onError(Throwable e) { | ||
listener.onCompressFailed(images, e.getMessage() + " is compress failures"); | ||
} | ||
}); | ||
} | ||
|
||
private void handleCompressCallBack(List<File> files) { | ||
for (int i = 0, j = images.size(); i < j; i++) { | ||
TImage image=images.get(i); | ||
image.setCompressed(true); | ||
image.setCompressPath(files.get(i).getPath()); | ||
} | ||
listener.onCompressSuccess(images); | ||
private void handleCompressCallBack(List<File> files) { | ||
for (int i = 0, j = images.size(); i < j; i++) { | ||
TImage image = images.get(i); | ||
image.setCompressed(true); | ||
image.setCompressPath(files.get(i).getPath()); | ||
} | ||
listener.onCompressSuccess(images); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
|
||
import java.io.Serializable; | ||
|
||
import me.shaohui.advancedluban.Luban; | ||
|
||
/** | ||
* Luban配置类 | ||
* Author: crazycodeboy | ||
|
@@ -13,76 +11,69 @@ | |
* GitHub:https://github.com/crazycodeboy | ||
* Eamil:[email protected] | ||
*/ | ||
public class LubanOptions implements Serializable{ | ||
/** | ||
* 压缩到的最大大小,单位B | ||
*/ | ||
private int maxSize; | ||
private int maxHeight; | ||
private int maxWidth; | ||
private int gear= Luban.CUSTOM_GEAR; | ||
private LubanOptions(){} | ||
|
||
public int getMaxSize() { | ||
return maxSize; | ||
} | ||
|
||
public void setMaxSize(int maxSize) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
public int getMaxHeight() { | ||
return maxHeight; | ||
} | ||
|
||
public void setMaxHeight(int maxHeight) { | ||
this.maxHeight = maxHeight; | ||
public class LubanOptions implements Serializable { | ||
/** | ||
* 压缩到的最大大小,单位B | ||
*/ | ||
private int maxSize; | ||
private int maxHeight; | ||
private int maxWidth; | ||
|
||
private LubanOptions() { | ||
} | ||
|
||
public int getMaxSize() { | ||
return maxSize; | ||
} | ||
|
||
public void setMaxSize(int maxSize) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
public int getMaxHeight() { | ||
return maxHeight; | ||
} | ||
|
||
public void setMaxHeight(int maxHeight) { | ||
this.maxHeight = maxHeight; | ||
} | ||
|
||
public int getMaxWidth() { | ||
return maxWidth; | ||
} | ||
|
||
public void setMaxWidth(int maxWidth) { | ||
this.maxWidth = maxWidth; | ||
} | ||
|
||
public static class Builder { | ||
private LubanOptions options; | ||
|
||
public Builder() { | ||
options = new LubanOptions(); | ||
} | ||
|
||
public int getMaxWidth() { | ||
return maxWidth; | ||
public Builder setMaxSize(int maxSize) { | ||
options.setMaxSize(maxSize); | ||
return this; | ||
} | ||
|
||
public void setMaxWidth(int maxWidth) { | ||
this.maxWidth = maxWidth; | ||
public Builder setMaxHeight(int maxHeight) { | ||
options.setMaxHeight(maxHeight); | ||
return this; | ||
} | ||
|
||
public int getGear() { | ||
return gear; | ||
public Builder setMaxWidth(int maxWidth) { | ||
options.setMaxWidth(maxWidth); | ||
return this; | ||
} | ||
|
||
public void setGear(int gear) { | ||
this.gear = gear; | ||
public Builder setGear(int gear) { | ||
return this; | ||
} | ||
|
||
public static class Builder{ | ||
private LubanOptions options; | ||
|
||
public Builder() { | ||
options=new LubanOptions(); | ||
} | ||
|
||
public Builder setMaxSize(int maxSize) { | ||
options.setMaxSize(maxSize); | ||
return this; | ||
} | ||
|
||
public Builder setMaxHeight(int maxHeight) { | ||
options.setMaxHeight(maxHeight); | ||
return this; | ||
} | ||
|
||
public Builder setMaxWidth(int maxWidth) { | ||
options.setMaxWidth(maxWidth); | ||
return this; | ||
} | ||
|
||
public Builder setGear(int gear) { | ||
options.setGear(gear); | ||
return this; | ||
} | ||
public LubanOptions create(){ | ||
return options; | ||
} | ||
public LubanOptions create() { | ||
return options; | ||
} | ||
} | ||
} |