Skip to content

Commit da1e8c6

Browse files
author
xiaoqi
committed
update to 1.1.6
1 parent 57ca932 commit da1e8c6

File tree

10 files changed

+100
-34
lines changed

10 files changed

+100
-34
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ a lightweight image compress framework for Android based on libJpeg.
3434
1.1.4 增加autoRotation的设置(只有压缩从本地读取图片有用,其他情况无效),可以将图片自动旋转为0度, 便于解决三星手机拍照会自动将图片选择的问题;
3535
1.1.5 优化部分代码,解决bug;
3636
增加autoRecycle的设置(只有压缩bitmap和压缩byte类型的图片有用,其他类型图片无效),开启该设置代表自动会将传入的bitmap或者bytes进行内存回收;
37-
37+
1.1.6 增加compressFileSize的压缩设置,可以设置当大于一定kb大小的图片,才会进行压缩,只对压缩保存到本地的方法有效,对压缩保存到bitmap的方法无效
3838

3939
### 使用方法:
4040

@@ -65,6 +65,7 @@ a lightweight image compress framework for Android based on libJpeg.
6565
4.ignoreSize: 是否要忽略压缩后图片宽高的限制, 如果设为true,压缩的时候会根据原图本身大小进行压缩,设置的width和height就会失效,默认false。
6666
5.autoRotation: 是否要将图片自动摆正,只有压缩从本地读取图片有用,其他情况无效(例如三星手机拍照后图片会自动旋转,设为true则会自动将图片旋转正确的方向)。
6767
6.autoRecycle: 是否需要自动将传入的bitmap或者bytes进行内存回收,只有压缩bitmap和压缩byte类型的图片有用,其他类型图片无效
68+
7.compressFileSize: 对压缩图片到本地的时候增加文件大小的设置,如果大于此kb大小的图片,则不进行压缩。
6869
因为从网络下载图片保存到本地,中间默认会自动压缩图片,如果不想对图片进行压缩,并保持宽高的话,设置如下参数即可:
6970

7071
CompressArgs args = new CompressArgs.Builder().quality(100).ignoreSize(true).autoRotation(true)
@@ -89,6 +90,7 @@ a lightweight image compress framework for Android based on libJpeg.
8990
lightConfig.setMaxHeight(xx);
9091
lightConfig.setMaxHeight(xx);
9192
lightConfig.setNeedIgnoreSize(xx);
93+
...
9294
Light.getInstance().setConfig(lightConfig);
9395

9496

@@ -153,11 +155,11 @@ a lightweight image compress framework for Android based on libJpeg.
153155
与上述方法类似,只需要compressForUri() 和 compressForString()传入要保存到的路径即可。同样支持uri和string的网络地址类型。
154156

155157
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/pic.jpg";
156-
Flowable.just(uri).compose(RxLight.compressForUriHttp(path)).subscribe(bitmap -> ivCompress.setImageBitmap(bitmap));
158+
Flowable.just(uri).compose(RxLight.compressForUriHttp(path)).subscribe(result -> {});
157159
//自定义压缩参数
158160
CompressArgs args = new CompressArgs();
159161
....
160-
Flowable.just(uri).compose(RxLight.compressForUriHttp(path, args)).subscribe(bitmap -> ivCompress.setImageBitmap(bitmap));
162+
Flowable.just(uri).compose(RxLight.compressForUriHttp(path, args)).subscribe(result -> {});
161163

162164
##### 通过本地资源异步压缩
163165
同样都支持 File,String,Uri,Bytes,Bitmap,DrawableResourceID,Drawable这几种类型

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ dependencies {
4747
testImplementation 'junit:junit:4.12'
4848
androidTestImplementation 'com.android.support.test:runner:1.0.1'
4949
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
50-
// implementation 'com.noober.light:core:1.1.5'
51-
implementation project(':light')
50+
implementation 'com.noober.light:core:1.1.6'
51+
// implementation project(':light')
5252
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
5353
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
5454
}

app/src/main/java/com/light/example/MainActivity.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
import com.light.core.Utils.UriParser;
2525

2626
import java.io.File;
27+
import java.util.List;
2728
import java.util.Locale;
2829

2930
import io.reactivex.Flowable;
30-
import io.reactivex.android.schedulers.AndroidSchedulers;
31-
import io.reactivex.schedulers.Schedulers;
3231

3332

3433
public class MainActivity extends AppCompatActivity {
@@ -38,7 +37,7 @@ public class MainActivity extends AppCompatActivity {
3837
TextView tvInfo1;
3938
TextView tvInfo2;
4039
Uri imageUri;
41-
String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/1213131.jpg";
40+
String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ttjpeg.jpg";
4241
final static String info1 = "压缩后:\n高度:%d,宽度:%d,占用内存:%dKB,文件大小:%dKB";
4342
final static String info2 = "原图片:\n高度:%d,宽度:%d,占用内存:%dKB,文件大小:%dKB";
4443

@@ -56,13 +55,6 @@ protected void onCreate(Bundle savedInstanceState) {
5655
tvInfo1 = findViewById(R.id.tv_info1);
5756
tvInfo2 = findViewById(R.id.tv_info2);
5857

59-
Flowable.just("http://img52.fooww.com:9999/group5/M01/00/99/ZyjCGlrtH9uEPlSIAAAAAN9Juaw140.jpg")
60-
.compose(RxLight.compressFoStringHttp(path1))
61-
.subscribeOn(Schedulers.io())
62-
.observeOn(AndroidSchedulers.mainThread())
63-
.subscribe(aBoolean -> {});
64-
65-
6658
// //Light1.2新增写法:
6759
// Bitmap bitmap1 = Light.getInstance().source(imageUri).width(500).height(500).autoRotation(true).compress();
6860
// boolean result1 = Light.getInstance().source(imageUri).width(500).height(500).autoRotation(true).compress(path);
@@ -102,8 +94,12 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
10294
options.inJustDecodeBounds = true;
10395
BitmapFactory.decodeFile(path, options);
10496
// Bitmap compressBitmap = Light.getInstance().compress(imageUri);
105-
Light.getInstance().compress(imageUri, path1);
106-
// ivCompress.setImageBitmap(compressBitmap);
97+
CompressArgs args = new CompressArgs.Builder().width(1600)
98+
.height(1600).autoRotation(true).compressFileSize(200)
99+
.build();
100+
Light.getInstance().compress(imageUri, args, path1);
101+
Bitmap compressBitmap = Light.getInstance().compress(path1);
102+
ivCompress.setImageBitmap(compressBitmap);
107103
Bitmap bitmap2 = BitmapFactory.decodeFile(path);
108104
ivImage.setImageBitmap(bitmap2);
109105
// tvInfo1.setVisibility(View.VISIBLE);

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ext {
3030
userOrg = 'noober'
3131
groupId = 'com.noober.light'
3232
uploadName = 'Light'
33-
publishVersion = '1.1.5'
33+
publishVersion = '1.1.6'
3434
desc = "a light weight image compress framework for Android based on libjpeg"
3535
website = 'https://github.com/JavaNoober/Light'
3636
// gradlew clean build bintrayUpload -PbintrayUser=xiaoqiandroid -PbintrayKey=xxxxxxxxxxxxxxxx -PdryRun=false

gradlew

100644100755
File mode changed.

light/src/main/java/com/light/body/CompressArgs.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CompressArgs {
1313
private boolean ignoreSize;
1414
private boolean autoRotation;
1515
private boolean autoRecycle;
16+
private int compressFileSize = -1;//kb
1617

1718
private CompressArgs(){
1819

@@ -49,10 +50,19 @@ public boolean isAutoRecycle() {
4950
return autoRecycle;
5051
}
5152

52-
public static class Builder {
53+
public int getCompressFileSize() {
54+
return compressFileSize;
55+
}
56+
57+
public void setCompressFileSize(int compressFileSize) {
58+
this.compressFileSize = compressFileSize;
59+
}
60+
61+
public static class Builder {
5362
private int width;
5463
private int height;
5564
private int quality;
65+
private int compressFileSize = Light.getInstance().getConfig().getCompressFileSize();
5666
private boolean autoRotation = Light.getInstance().getConfig().isAutoRotation();
5767
private boolean ignoreSize = Light.getInstance().getConfig().isNeedIgnoreSize();
5868
private boolean autoRecycle = Light.getInstance().getConfig().isAutoRecycle();
@@ -87,6 +97,11 @@ public Builder autoRecycle(boolean autoRecycle) {
8797
return this;
8898
}
8999

100+
public Builder compressFileSize(int compressFileSize){
101+
this.compressFileSize = compressFileSize;
102+
return this;
103+
}
104+
90105
public CompressArgs build(){
91106
CompressArgs args = new CompressArgs();
92107
args.width = width;
@@ -95,6 +110,7 @@ public CompressArgs build(){
95110
args.ignoreSize = ignoreSize;
96111
args.autoRotation = autoRotation;
97112
args.autoRecycle = autoRecycle;
113+
args.compressFileSize = compressFileSize;
98114
return args;
99115
}
100116
}

light/src/main/java/com/light/body/LightConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class LightConfig implements Serializable {
1212

1313
private static final long serialVersionUID = 1L;
1414

15-
private int maxFileSize = 500;
15+
private int compressFileSize = -1;//单位kb
1616

1717
private String outputRootDir;
1818

1919
private int maxWidth;
2020

2121
private int maxHeight;
2222

23-
private int defaultQuality = 85;
23+
private int defaultQuality = 70;
2424

2525
private boolean needIgnoreSize = false;
2626

@@ -35,8 +35,8 @@ public LightConfig() {
3535
maxHeight = DisplayUtil.getScreenHeight(Light.getInstance().getContext());
3636
}
3737

38-
public LightConfig setMaxFileSize(int maxFileSize) {
39-
this.maxFileSize = maxFileSize;
38+
public LightConfig setCompressFileSize(int compressFileSize) {
39+
this.compressFileSize = compressFileSize;
4040
return this;
4141
}
4242

@@ -45,8 +45,8 @@ public LightConfig setOutputRootDir(String outputRootDir) {
4545
return this;
4646
}
4747

48-
public int getMaxFileSize() {
49-
return maxFileSize;
48+
public int getCompressFileSize() {
49+
return compressFileSize;
5050
}
5151

5252
public String getOutputRootDir() {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.light.core.Utils;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.nio.channels.FileChannel;
8+
9+
/**
10+
* Created by xiaoqi on 2018/5/17
11+
*/
12+
public class FileUtils {
13+
14+
public static boolean copyFile(File source, File dest) throws IOException {
15+
boolean result = false;
16+
FileChannel inputChannel = null;
17+
FileChannel outputChannel = null;
18+
try {
19+
inputChannel = new FileInputStream(source).getChannel();
20+
outputChannel = new FileOutputStream(dest).getChannel();
21+
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
22+
result = true;
23+
} finally {
24+
if(inputChannel != null){
25+
inputChannel.close();
26+
}
27+
if(outputChannel != null){
28+
outputChannel.close();
29+
}
30+
}
31+
32+
return result;
33+
}
34+
}

light/src/main/java/com/light/proxy/BitmapCompressProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public boolean compress(String outPath) {
3535
if(outPath == null){
3636
outPath = lightConfig.getOutputRootDir();
3737
}
38-
return compressEngine.compress2File(compress(), outPath, quality);
38+
return compressEngine.compress2File(compress(), outPath, quality);
3939
}
4040

4141
@Override

light/src/main/java/com/light/proxy/FileCompressProxy.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
import com.light.body.LightConfig;
1010
import com.light.core.LightCompressEngine;
1111
import com.light.core.Utils.DegreeHelper;
12+
import com.light.core.Utils.FileUtils;
13+
import com.light.core.Utils.L;
1214
import com.light.core.Utils.MatrixUtil;
1315
import com.light.core.Utils.http.HttpDownLoader;
1416
import com.light.core.listener.ICompressEngine;
1517
import com.light.core.listener.ICompressProxy;
1618
import com.light.core.listener.OnCompressFinishListener;
1719

1820
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
1923

2024
/**
2125
* Created by xiaoqi on 2017/11/25
@@ -42,14 +46,28 @@ public boolean compress(String outPath) {
4246
if(outPath == null){
4347
outPath = lightConfig.getOutputRootDir();
4448
}
45-
Bitmap bitmap = compress();
46-
try {
47-
return compressEngine.compress2File(bitmap, outPath, quality);
48-
}finally {
49-
if(bitmap != null && !bitmap.isRecycled()){
50-
bitmap.recycle();
51-
}
52-
}
49+
int compressFileSize = lightConfig.getCompressFileSize();
50+
if(compressArgs.getCompressFileSize() > 0){
51+
compressFileSize = compressArgs.getCompressFileSize();
52+
}
53+
54+
if(compressFileSize > 0 && new File(path).length() / 1024 < compressFileSize){
55+
try {
56+
L.d("copyFile");
57+
return FileUtils.copyFile(new File(path), new File(outPath));
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
Bitmap bitmap = compress();
63+
try {
64+
return compressEngine.compress2File(bitmap, outPath, quality);
65+
}finally {
66+
if(bitmap != null && !bitmap.isRecycled()){
67+
bitmap.recycle();
68+
}
69+
}
70+
5371
}
5472

5573
@Override

0 commit comments

Comments
 (0)