Skip to content

Commit

Permalink
优化和新增开放接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Gru110110110 committed Jul 20, 2017
1 parent 7a0cf95 commit ecbc967
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Step 1. Add it in your root build.gradle at the end of repositories:
Step 2. Add the dependency
```gradle
dependencies {
       compile 'com.github.pruas:Biscuit:v1.0.5'
       compile 'com.github.pruas:Biscuit:v1.0.6'
}
```
Step 3. Use it wherever you need
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/java/com/seek/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MainActivity extends AppCompatActivity {
TextView mTextView;
StringBuilder info = null;
CompressListener mCompressListener;
Biscuit mBiscuit;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -45,7 +46,7 @@ public void onSuccess(String compressedPath) {
}

@Override
public void onError(Exception e) {
public void onError(CompressException e) {
CompressException err = (CompressException) e;
Log.e(">>>>>", "message : "+e.getMessage()+" original Path : "+err.originalPath);
}
Expand All @@ -69,19 +70,25 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
ArrayList<String> photos =
data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
Glide.clear(mImageView);
Glide.with(this).load(photos.get(0)).into(mImageView);
Biscuit.with(this)
.path(photos) //可以传入一张图片路径,也可以传入一个图片路径列表
.loggingEnabled(true)//是否输出log 默认输出
if (mBiscuit==null) {
mBiscuit = Biscuit.with(this)
.path(photos) //可以传入一张图片路径,也可以传入一个图片路径列表
.loggingEnabled(true)//是否输出log 默认输出
// .quality(50)//质量压缩值(0...100)默认已经非常接近微信,所以没特殊需求可以不用自定义
// .originalName(true) //使用原图名字来命名压缩后的图片,默认不使用原图名字,随机图片名字
.listener(mCompressListener)//压缩监听
.targetDir(FileUtils.getImageDir())//自定义压缩保存路径
.listener(mCompressListener)//压缩监听
.targetDir(FileUtils.getImageDir())//自定义压缩保存路径
// .executor(executor) //自定义实现执行,注意:必须在子线程中执行 默认使用AsyncTask线程池执行
// .ignoreAlpha(true)//忽略alpha通道,对图片没有透明度要求可以这么做,默认不忽略。
// .compressType(Biscuit.SAMPLE)//采用采样率压缩方式,默认是使用缩放压缩方式,也就是和微信的一样。
.ignoreLessThan(100)//忽略小于100kb的图片不压缩,返回原图路径
.build();
.ignoreLessThan(100)//忽略小于100kb的图片不压缩,返回原图路径
.build();
}else {
mBiscuit.addPaths(photos);//传入压缩列表
mBiscuit.compress();//开始压缩
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0.5"
versionName "1.0.6"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
122 changes: 118 additions & 4 deletions library/src/main/java/com/seek/biscuit/Biscuit.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,123 @@ public class Biscuit {
private final static String TAG = "Biscuit";
public final static int SCALE = 0;
public final static int SAMPLE = 1;
Dispatcher mDispatcher;
Executor mExecutor;
String targetDir;
boolean ignoreAlpha;
int quality;
int compressType;
boolean useOriginalName;
long thresholdSize;
ArrayList<CompressListener> mCompressListeners;
ArrayList<String> mPaths;

Biscuit(ArrayList<String> paths, String targetDir, boolean ignoreAlpha, int quality, int compressType, boolean useOriginalName, boolean loggingEnabled, long thresholdSize, CompressListener compressListener, Executor executor) {
Utils.loggingEnabled = loggingEnabled;
Dispatcher dispatcher = new Dispatcher();
Iterator<String> iterator = paths.iterator();
mDispatcher = new Dispatcher();
mExecutor = executor;
mCompressListeners = new ArrayList<>();
addListener(compressListener);
mPaths = new ArrayList<>();
mPaths.addAll(paths);
this.targetDir = targetDir;
this.ignoreAlpha = ignoreAlpha;
this.quality = quality;
this.compressType = compressType;
this.useOriginalName = useOriginalName;
this.thresholdSize = thresholdSize;
compress();
}

public void compress() {
Iterator<String> iterator = mPaths.iterator();
while (iterator.hasNext()) {
String path = iterator.next();
if (Utils.isImage(path)) {
Compressor compressor = new ImageCompressor(path, targetDir, quality, compressType, ignoreAlpha, useOriginalName, thresholdSize, dispatcher, compressListener);
executor.execute(compressor);
Compressor compressor = new ImageCompressor(path, targetDir, quality, compressType, ignoreAlpha, useOriginalName, thresholdSize, this);
mExecutor.execute(compressor);
} else {
log(TAG, "can not recognize the path : " + path);
}
iterator.remove();
}
}

public void addListener(CompressListener compressListener) {
mCompressListeners.add(compressListener);
}

public void removeListener(CompressListener compressListener) {
mCompressListeners.remove(compressListener);
}

public String getTargetDir() {
return targetDir;
}

public void setTargetDir(String targetDir) {
this.targetDir = targetDir;
}

public boolean isIgnoreAlpha() {
return ignoreAlpha;
}

public void setIgnoreAlpha(boolean ignoreAlpha) {
this.ignoreAlpha = ignoreAlpha;
}

public int getQuality() {
return quality;
}

public void setQuality(int quality) {
if (quality < 0 || quality > 100) {
throw new IllegalArgumentException("quality must be 0..100");
}
this.quality = quality;
}

public int getCompressType() {
return compressType;
}

public void setCompressType(@CompressType int compressType) {
this.compressType = compressType;
}

public boolean isUseOriginalName() {
return useOriginalName;
}

public void setUseOriginalName(boolean useOriginalName) {
this.useOriginalName = useOriginalName;
}

public long getThresholdSize() {
return thresholdSize;
}

public void setThresholdSize(long thresholdSize) {
this.thresholdSize = thresholdSize;
}

public ArrayList<String> getPaths() {
return mPaths;
}

public void addPaths(ArrayList<String> paths) {
if (paths != null && paths.size() > 0) {
mPaths.addAll(paths);
}
}

public void addPaths(String path) {
if (!TextUtils.isEmpty(path)) {
mPaths.add(path);
}
}

public static Builder with(Context context) {
return new Builder(context);
}
Expand All @@ -51,6 +151,20 @@ public static void clearCache(String dir) {
Utils.clearCache(dir);
}

public void dispatchSuccess(String targetPath) {
for (CompressListener compressListener :
mCompressListeners) {
compressListener.onSuccess(targetPath);
}
}

public void dispatchError(CompressException exception) {
for (CompressListener compressListener :
mCompressListeners) {
compressListener.onError(exception);
}
}

@IntDef({SAMPLE, SCALE})
@Retention(RetentionPolicy.SOURCE)
public @interface CompressType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface CompressListener {

void onSuccess(String compressedPath);

void onError(Exception e);
void onError(CompressException e);
}
5 changes: 2 additions & 3 deletions library/src/main/java/com/seek/biscuit/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class Dispatcher {
private final Handler mHandler;
private static final int MESSAGE_COMPLETE = 0x1;
private static final int MESSAGE_ERROR = 0x2;

public Dispatcher() {
mHandler = new DispatchHandler(Looper.getMainLooper());
}
Expand All @@ -38,10 +37,10 @@ public void handleMessage(Message msg) {
ImageCompressor compressor = (ImageCompressor) msg.obj;
switch (msg.what) {
case MESSAGE_COMPLETE:
compressor.compressListener.onSuccess(compressor.targetPath);
compressor.mBiscuit.dispatchSuccess(compressor.targetPath);
break;
case MESSAGE_ERROR:
compressor.compressListener.onError(compressor.exception);
compressor.mBiscuit.dispatchError(compressor.exception);
break;
}
}
Expand Down
18 changes: 8 additions & 10 deletions library/src/main/java/com/seek/biscuit/ImageCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ public class ImageCompressor implements Compressor {
private String targetDir;
private int quality;
private int compressType;
private Dispatcher dispatcher;
private boolean ignoreAlpha;
private boolean useOriginalName;
private long thresholdSize;
CompressListener compressListener;
String targetPath;
Exception exception;
CompressException exception;
final Biscuit mBiscuit;

public ImageCompressor(String path, String targetDir, int quality, @Biscuit.CompressType int compressType, boolean ignoreAlpha, boolean useOriginalName, long thresholdSize, Dispatcher dispatcher, CompressListener compressListener) {
public ImageCompressor(String path, String targetDir, int quality, @Biscuit.CompressType int compressType, boolean ignoreAlpha, boolean useOriginalName, long thresholdSize, Biscuit biscuit) {
this.sourcePath = new ImagePath(path);
this.targetDir = targetDir;
this.quality = quality;
this.compressType = compressType;
this.dispatcher = dispatcher;
this.compressListener = compressListener;
this.ignoreAlpha = ignoreAlpha;
this.useOriginalName = useOriginalName;
this.thresholdSize = thresholdSize;
this.mBiscuit = biscuit;
}

@Override
Expand Down Expand Up @@ -169,14 +167,14 @@ private boolean checkOriginalLength() {
}

private void dispatchSuccess() {
if (dispatcher != null && compressListener != null) {
dispatcher.dispatchComplete(this);
if (mBiscuit != null) {
mBiscuit.mDispatcher.dispatchComplete(this);
}
}

private void dispatchError() {
if (dispatcher != null && compressListener != null) {
dispatcher.dispatchError(this);
if (mBiscuit != null) {
mBiscuit.mDispatcher.dispatchError(this);
}
}

Expand Down

0 comments on commit ecbc967

Please sign in to comment.