From 5ab28e3cbdb901803dc1061416dff88cc851977f Mon Sep 17 00:00:00 2001 From: little-snow-fox Date: Sun, 24 Nov 2019 00:35:21 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BC=98=E5=8C=96=E5=AE=89=E5=8D=93bitm?= =?UTF-8?q?apResizeGetBytes=E6=96=B9=E6=B3=95=E7=9A=84=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E9=80=BB=E8=BE=91=EF=BC=8C=E6=8F=90=E9=AB=98?= =?UTF-8?q?=E7=BC=A9=E7=95=A5=E5=9B=BE=E7=9A=84=E6=B8=85=E6=99=B0=E5=BA=A6?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/theweflex/react/WeChatModule.java | 15 ++++++++++----- package.json | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/theweflex/react/WeChatModule.java b/android/src/main/java/com/theweflex/react/WeChatModule.java index 91da5da..aee5a34 100644 --- a/android/src/main/java/com/theweflex/react/WeChatModule.java +++ b/android/src/main/java/com/theweflex/react/WeChatModule.java @@ -74,19 +74,24 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv } 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(); // 质量压缩方法,这里100表示第一次不压缩,把压缩后的数据缓存到 baos image.compress(Bitmap.CompressFormat.JPEG, 100, baos); - int options = 10; + int options = 100; // 循环判断压缩后依然大于 32kb 则继续压缩 while (baos.toByteArray().length / 1024 > size) { // 重置baos即清空baos baos.reset(); - // 每次都减少1 - options += 1; + if (options <= 1) { + break; + } else if (options > 5) { + options -= 5; + } else { + options -= 1; + } // 这里压缩options%,把压缩后的数据存放到baos中 - image.compress(Bitmap.CompressFormat.JPEG, 10 / options * 10, baos); + image.compress(Bitmap.CompressFormat.JPEG, options, baos); } return baos.toByteArray(); } diff --git a/package.json b/package.json index 1a7c2eb..2472fbb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-wechat-lib", - "version": "1.1.5", + "version": "1.1.7", "description": "react-native library for wechat app. 支持分享和拉起小程序。", "main": "index.js", "scripts": {