作者 yang

update 更新

@@ -3,6 +3,7 @@ package com.theweflex.react; @@ -3,6 +3,7 @@ package com.theweflex.react;
3 import android.content.Context; 3 import android.content.Context;
4 import android.content.Intent; 4 import android.content.Intent;
5 import android.graphics.Bitmap; 5 import android.graphics.Bitmap;
  6 +import android.graphics.BitmapFactory;
6 import android.net.Uri; 7 import android.net.Uri;
7 import android.support.annotation.Nullable; 8 import android.support.annotation.Nullable;
8 9
@@ -46,6 +47,7 @@ import com.tencent.mm.opensdk.openapi.IWXAPI; @@ -46,6 +47,7 @@ import com.tencent.mm.opensdk.openapi.IWXAPI;
46 import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; 47 import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
47 import com.tencent.mm.opensdk.openapi.WXAPIFactory; 48 import com.tencent.mm.opensdk.openapi.WXAPIFactory;
48 49
  50 +import java.io.ByteArrayOutputStream;
49 import java.util.ArrayList; 51 import java.util.ArrayList;
50 import java.util.UUID; 52 import java.util.UUID;
51 53
@@ -60,6 +62,46 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv @@ -60,6 +62,46 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
60 private final static String INVOKE_FAILED = "WeChat API invoke returns false."; 62 private final static String INVOKE_FAILED = "WeChat API invoke returns false.";
61 private final static String INVALID_ARGUMENT = "invalid argument."; 63 private final static String INVALID_ARGUMENT = "invalid argument.";
62 64
  65 +
  66 + // 缩略图大小 kb
  67 + private final static int THUMB_SIZE = 32;
  68 +
  69 + private static byte[] bitmapTopBytes(Bitmap bitmap) {
  70 + ByteArrayOutputStream baos = new ByteArrayOutputStream();
  71 + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  72 + bitmap.recycle();
  73 + return baos.toByteArray();
  74 + }
  75 + private static byte[] bitmapResizeGetBytes(Bitmap image, int size) {
  76 + // little-snow-fox 2019.10.20
  77 + ByteArrayOutputStream baos = new ByteArrayOutputStream();
  78 + // 质量压缩方法,这里100表示第一次不压缩,把压缩后的数据缓存到 baos
  79 + image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  80 + int options = 100;
  81 +
  82 +
  83 + // 循环判断压缩后依然大于 32kb 则继续压缩
  84 + while (baos.toByteArray().length / 1024 > size) {
  85 + // 重置baos即清空baos
  86 + baos.reset();
  87 + if (options > 10) {
  88 + options -= 8;
  89 + } else {
  90 + return bitmapResizeGetBytes(Bitmap.createScaledBitmap(image, 280, image.getHeight() / image.getWidth() * 280, true), size);
  91 + }
  92 + // 这里压缩options%,把压缩后的数据存放到baos中
  93 + image.compress(Bitmap.CompressFormat.JPEG, options, baos);
  94 + }
  95 + return baos.toByteArray();
  96 + }
  97 +
  98 + private Bitmap Bytes2Bimap(byte[] b) {
  99 + if (b.length != 0) {
  100 + return BitmapFactory.decodeByteArray(b, 0, b.length);
  101 + } else {
  102 + return null;
  103 + }
  104 + }
63 public WeChatModule(ReactApplicationContext context) { 105 public WeChatModule(ReactApplicationContext context) {
64 super(context); 106 super(context);
65 } 107 }
@@ -425,6 +467,7 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv @@ -425,6 +467,7 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
425 message.mediaObject = mediaObject; 467 message.mediaObject = mediaObject;
426 468
427 if (thumbImage != null) { 469 if (thumbImage != null) {
  470 + thumbImage=Bytes2Bimap(bitmapResizeGetBytes(thumbImage, THUMB_SIZE));
428 message.setThumbImage(thumbImage); 471 message.setThumbImage(thumbImage);
429 } 472 }
430 if (data.hasKey("title")) { 473 if (data.hasKey("title")) {