Skip to content

Commit 5ab28e3

Browse files
feat 优化安卓bitmapResizeGetBytes方法的图片压缩逻辑,提高缩略图的清晰度度
1 parent 0764841 commit 5ab28e3

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

android/src/main/java/com/theweflex/react/WeChatModule.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,24 @@ private static byte[] bitmapTopBytes(Bitmap bitmap) {
7474
}
7575

7676
private static byte[] bitmapResizeGetBytes(Bitmap image, int size) {
77-
// 这个压缩算法存在效率问题,希望有义士可以出手优化 by little-snow-fox 2019.10.20
77+
// little-snow-fox 2019.10.20
7878
ByteArrayOutputStream baos = new ByteArrayOutputStream();
7979
// 质量压缩方法,这里100表示第一次不压缩,把压缩后的数据缓存到 baos
8080
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
81-
int options = 10;
81+
int options = 100;
8282
// 循环判断压缩后依然大于 32kb 则继续压缩
8383
while (baos.toByteArray().length / 1024 > size) {
8484
// 重置baos即清空baos
8585
baos.reset();
86-
// 每次都减少1
87-
options += 1;
86+
if (options <= 1) {
87+
break;
88+
} else if (options > 5) {
89+
options -= 5;
90+
} else {
91+
options -= 1;
92+
}
8893
// 这里压缩options%,把压缩后的数据存放到baos中
89-
image.compress(Bitmap.CompressFormat.JPEG, 10 / options * 10, baos);
94+
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
9095
}
9196
return baos.toByteArray();
9297
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-wechat-lib",
3-
"version": "1.1.5",
3+
"version": "1.1.7",
44
"description": "react-native library for wechat app. 支持分享和拉起小程序。",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)