Browse Source

1、提供同步方法
2、修复文件同名问题

郑梓斌 7 years ago
parent
commit
c627354bb9

+ 25 - 5
README.md

@@ -37,22 +37,25 @@ compile 'top.zibin:Luban:1.1.0'
 
 # Release Notes
 
+### [v1.1.1, 2017/6/13](https://github.com/Curzibn/Luban/milestone/1)
+
+- 修改压缩方式,压缩速度更快、压缩质量更换
+- 去掉rxJava依赖,提供同步压缩方法
+
 ### [v1.0.9, 2016/10/14](https://github.com/Curzibn/Luban/milestone/1)
 
 - 修改压缩后文件自带后缀,根据([#77](https://github.com/Curzibn/Luban/issues/77))提供的思路
 
 # 使用
 
-### Listener方式
+### 异步调用
 
 `Luban`内部采用`IO`线程进行图片压缩,外部调用只需设置好结果监听即可:
 
 ```java
-Luban.get(this)
+Luban.with(this)
     .load(File)                     //传人要压缩的图片
-    .putGear(Luban.THIRD_GEAR)      //设定压缩档次,默认三挡
     .setCompressListener(new OnCompressListener() { //设置回调
-
         @Override
         public void onStart() {
             // TODO 压缩开始前调用,可以在方法内启动 loading UI
@@ -64,11 +67,28 @@ Luban.get(this)
 
         @Override
         public void onError(Throwable e) {
-            // TODO 当压缩过出现问题时调用
+            // TODO 当压缩过出现问题时调用
         }
     }).launch();    //启动压缩
 ```
 
+### 同步调用
+
+同步方法请尽量避免在主线程调用以免阻塞主线程,下面以rxJava调用为例
+
+```java
+Flowable.just(file)
+    .observeOn(Schedulers.io())
+    .map(new Function<File, File>() {
+      @Override public File apply(@NonNull File file) throws Exception {
+        // 同步方法直接返回压缩后的文件
+        return Luban.with(MainActivity.this).load(file).get();
+      }
+    })
+    .observeOn(AndroidSchedulers.mainThread())
+    .subscribe();
+```
+
 # License
 
     Copyright 2016 Zheng Zibin

+ 4 - 2
example/build.gradle

@@ -24,11 +24,13 @@ dependencies {
     testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:25.3.1'
     compile 'com.android.support:design:25.3.1'
-    compile 'io.reactivex:rxandroid:1.1.0'
-    compile 'io.reactivex:rxjava:1.1.3'
 
     compile 'com.nineoldandroids:library:2.4.0'
     compile 'com.github.bumptech.glide:glide:3.7.0'
     compile 'me.iwf.photopicker:PhotoPicker:0.9.5@aar'
+
+    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
+    compile 'io.reactivex.rxjava2:rxjava:2.1.0'
+
     compile project(':library')
 }

+ 48 - 19
example/src/main/java/top/zibin/luban/example/MainActivity.java

@@ -17,11 +17,18 @@ import com.bumptech.glide.Glide;
 import java.io.File;
 import java.util.ArrayList;
 
+import io.reactivex.Flowable;
+import io.reactivex.android.schedulers.AndroidSchedulers;
+import io.reactivex.annotations.NonNull;
+import io.reactivex.functions.Consumer;
+import io.reactivex.functions.Function;
+import io.reactivex.schedulers.Schedulers;
 import me.iwf.photopicker.PhotoPicker;
 import top.zibin.luban.Luban;
 import top.zibin.luban.OnCompressListener;
 
 public class MainActivity extends AppCompatActivity {
+  private static final String TAG = "Luban";
 
   private TextView fileSize;
   private TextView imageSize;
@@ -56,11 +63,51 @@ public class MainActivity extends AppCompatActivity {
     });
   }
 
+  @Override
+  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+    super.onActivityResult(requestCode, resultCode, data);
+
+    if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
+      if (data != null) {
+        ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
+
+        File imgFile = new File(photos.get(0));
+        fileSize.setText(imgFile.length() / 1024 + "k");
+        imageSize.setText(computeSize(imgFile)[0] + "*" + computeSize(imgFile)[1]);
+
+        for (String photo : photos) {
+          compressWithRx(new File(photo));
+        }
+      }
+    }
+  }
+
+  private void compressWithRx(File file) {
+    Flowable.just(file)
+        .observeOn(Schedulers.io())
+        .map(new Function<File, File>() {
+          @Override public File apply(@NonNull File file) throws Exception {
+            return Luban.with(MainActivity.this).load(file).get();
+          }
+        })
+        .observeOn(AndroidSchedulers.mainThread())
+        .subscribe(new Consumer<File>() {
+          @Override public void accept(@NonNull File file) throws Exception {
+            Log.d(TAG, file.getAbsolutePath());
+
+            Glide.with(MainActivity.this).load(file).into(image);
+
+            thumbFileSize.setText(file.length() / 1024 + "k");
+            thumbImageSize.setText(computeSize(file)[0] + "*" + computeSize(file)[1]);
+          }
+        });
+  }
+
   /**
    * 压缩单张图片 Listener 方式
    */
   private void compressWithLs(File file) {
-    Luban.get(this)
+    Luban.with(this)
         .load(file)
         .setCompressListener(new OnCompressListener() {
           @Override
@@ -98,22 +145,4 @@ public class MainActivity extends AppCompatActivity {
 
     return size;
   }
-
-  @Override
-  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-    super.onActivityResult(requestCode, resultCode, data);
-
-    if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
-      if (data != null) {
-        ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
-
-        File imgFile = new File(photos.get(0));
-        fileSize.setText(imgFile.length() / 1024 + "k");
-        imageSize.setText(computeSize(imgFile)[0] + "*" + computeSize(imgFile)[1]);
-
-        for (int i = 0; i < photos.size(); i++)
-          compressWithLs(new File(photos.get(i)));
-      }
-    }
-  }
 }

+ 9 - 1
library/src/main/java/top/zibin/luban/Engine.java

@@ -21,7 +21,9 @@ class Engine {
   private int srcHeight;
 
   Engine(File srcImg, File tagImg) throws IOException {
-    this.srcExif = new ExifInterface(srcImg.getAbsolutePath());
+    if (isJpeg(srcImg)) {
+      this.srcExif = new ExifInterface(srcImg.getAbsolutePath());
+    }
     this.tagImg = tagImg;
     this.srcImg = srcImg;
 
@@ -34,6 +36,10 @@ class Engine {
     this.srcHeight = options.outHeight;
   }
 
+  private boolean isJpeg(File photo) {
+    return photo.getAbsolutePath().contains("jpeg") || photo.getAbsolutePath().contains("jpg");
+  }
+
   private int computeSize() {
     int mSampleSize;
 
@@ -65,6 +71,8 @@ class Engine {
   }
 
   private Bitmap rotatingImage(Bitmap bitmap) {
+    if (srcExif == null) return bitmap;
+
     Matrix matrix = new Matrix();
     int angle = 0;
     int orientation = srcExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

+ 21 - 8
library/src/main/java/top/zibin/luban/Luban.java

@@ -6,6 +6,7 @@ import android.os.Looper;
 import android.os.Message;
 import android.support.annotation.Nullable;
 import android.support.annotation.UiThread;
+import android.support.annotation.WorkerThread;
 import android.util.Log;
 
 import java.io.File;
@@ -30,7 +31,7 @@ public class Luban implements Handler.Callback {
     mHandler = new Handler(Looper.getMainLooper(), this);
   }
 
-  public static Builder get(Context context) {
+  public static Builder with(Context context) {
     return new Builder(context);
   }
 
@@ -42,7 +43,7 @@ public class Luban implements Handler.Callback {
    */
   private File getImageCacheFile(Context context) {
     if (getImageCacheDir(context) != null) {
-      return new File(getImageCacheDir(context) + "/" + System.currentTimeMillis());
+      return new File(getImageCacheDir(context) + "/" + System.currentTimeMillis() + (int) (Math.random() * 100));
     }
     return null;
   }
@@ -54,7 +55,7 @@ public class Luban implements Handler.Callback {
    * @param context
    *     A context.
    *
-   * @see #getImageCacheDir(android.content.Context, String)
+   * @see #getImageCacheDir(Context, String)
    */
   @Nullable
   private File getImageCacheDir(Context context) {
@@ -70,7 +71,7 @@ public class Luban implements Handler.Callback {
    * @param cacheName
    *     The name of the subdirectory in which to store the cache.
    *
-   * @see #getImageCacheDir(android.content.Context)
+   * @see #getImageCacheDir(Context)
    */
   @Nullable
   private File getImageCacheDir(Context context, String cacheName) {
@@ -89,6 +90,9 @@ public class Luban implements Handler.Callback {
     return null;
   }
 
+  /**
+   * start asynchronous compress thread
+   */
   @UiThread private void launch(final Context context) {
     if (file == null && onCompressListener != null) {
       onCompressListener.onError(new NullPointerException("image file cannot be null"));
@@ -108,6 +112,13 @@ public class Luban implements Handler.Callback {
     }).start();
   }
 
+  /**
+   * start compress and return the file
+   */
+  @WorkerThread private File get(final Context context) throws IOException {
+    return new Engine(file, getImageCacheFile(context)).compress();
+  }
+
   @Override public boolean handleMessage(Message msg) {
     if (onCompressListener == null) return false;
 
@@ -152,10 +163,12 @@ public class Luban implements Handler.Callback {
       return this;
     }
 
-    public Luban launch() {
-      Luban luban = build();
-      luban.launch(context);
-      return luban;
+    public void launch() {
+      build().launch(context);
+    }
+
+    public File get() throws IOException {
+      return build().get(context);
     }
   }
 }