feat 优化安卓bitmapResizeGetBytes方法的图片压缩逻辑,提高缩略图的清晰度度

This commit is contained in:
little-snow-fox 2019-11-24 00:35:21 +08:00
parent 0764841ccb
commit 5ab28e3cbd
2 changed files with 11 additions and 6 deletions

View File

@ -74,19 +74,24 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
} }
private static byte[] bitmapResizeGetBytes(Bitmap image, int size) { private static byte[] bitmapResizeGetBytes(Bitmap image, int size) {
// 这个压缩算法存在效率问题希望有义士可以出手优化 by little-snow-fox 2019.10.20 // little-snow-fox 2019.10.20
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 质量压缩方法这里100表示第一次不压缩把压缩后的数据缓存到 baos // 质量压缩方法这里100表示第一次不压缩把压缩后的数据缓存到 baos
image.compress(Bitmap.CompressFormat.JPEG, 100, baos); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 10; int options = 100;
// 循环判断压缩后依然大于 32kb 则继续压缩 // 循环判断压缩后依然大于 32kb 则继续压缩
while (baos.toByteArray().length / 1024 > size) { while (baos.toByteArray().length / 1024 > size) {
// 重置baos即清空baos // 重置baos即清空baos
baos.reset(); baos.reset();
// 每次都减少1 if (options <= 1) {
options += 1; break;
} else if (options > 5) {
options -= 5;
} else {
options -= 1;
}
// 这里压缩options%把压缩后的数据存放到baos中 // 这里压缩options%把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, 10 / options * 10, baos); image.compress(Bitmap.CompressFormat.JPEG, options, baos);
} }
return baos.toByteArray(); return baos.toByteArray();
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "react-native-wechat-lib", "name": "react-native-wechat-lib",
"version": "1.1.5", "version": "1.1.7",
"description": "react-native library for wechat app. 支持分享和拉起小程序。", "description": "react-native library for wechat app. 支持分享和拉起小程序。",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {