feat 添加图片压缩参数,按宽度压缩,优化已有的图片压缩算法抛弃多余压缩次数

This commit is contained in:
little-snow-fox 2019-12-07 19:17:14 +08:00
parent 5ab28e3cbd
commit bdfaa21eb5
2 changed files with 17 additions and 8 deletions

View File

@ -83,12 +83,10 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
while (baos.toByteArray().length / 1024 > size) { while (baos.toByteArray().length / 1024 > size) {
// 重置baos即清空baos // 重置baos即清空baos
baos.reset(); baos.reset();
if (options <= 1) { if (options > 10) {
break; options -= 8;
} else if (options > 5) {
options -= 5;
} else { } else {
options -= 1; return bitmapResizeGetBytes(Bitmap.createScaledBitmap(image, 280, image.getHeight() / image.getWidth() * 280, true), size);
} }
// 这里压缩options%把压缩后的数据存放到baos中 // 这里压缩options%把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, options, baos); image.compress(Bitmap.CompressFormat.JPEG, options, baos);
@ -225,7 +223,13 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
public void shareImage(final ReadableMap data, final Callback callback) { public void shareImage(final ReadableMap data, final Callback callback) {
this._getImage(Uri.parse(data.getString("imageUrl")), null, new ImageCallback() { this._getImage(Uri.parse(data.getString("imageUrl")), null, new ImageCallback() {
@Override @Override
public void invoke(@Nullable Bitmap bmp) { public void invoke(@Nullable Bitmap bitmap) {
Bitmap bmp = bitmap;
int maxWidth = data.hasKey("maxWidth") ? data.getInt("maxWidth") : -1;
// 如果图片大于10MB而且没设置压缩自动开启压缩
if (maxWidth > 0) {
bmp = Bitmap.createScaledBitmap(bmp, maxWidth, bmp.getHeight() / bmp.getWidth() * maxWidth, true);
}
// 初始化 WXImageObject WXMediaMessage 对象 // 初始化 WXImageObject WXMediaMessage 对象
WXImageObject imgObj = new WXImageObject(bmp); WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage(); WXMediaMessage msg = new WXMediaMessage();
@ -259,13 +263,17 @@ public class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
if (path.indexOf("file://") > -1) { if (path.indexOf("file://") > -1) {
path = path.substring(7); path = path.substring(7);
} }
int maxWidth = data.hasKey("maxWidth") ? data.getInt("maxWidth") : -1;
fs = new FileInputStream(path); fs = new FileInputStream(path);
Bitmap bmp = BitmapFactory.decodeStream(fs); Bitmap bmp = BitmapFactory.decodeStream(fs);
// 如果图片大于10MB而且没设置压缩自动开启压缩
if (maxWidth > 0) {
bmp = Bitmap.createScaledBitmap(bmp, maxWidth, bmp.getHeight() / bmp.getWidth() * maxWidth, true);
}
// 初始化 WXImageObject WXMediaMessage 对象 // 初始化 WXImageObject WXMediaMessage 对象
WXImageObject imgObj = new WXImageObject(bmp); WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage(); WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj; msg.mediaObject = imgObj;
// 设置缩略图 // 设置缩略图
msg.thumbData = bitmapResizeGetBytes(bmp, THUMB_SIZE); msg.thumbData = bitmapResizeGetBytes(bmp, THUMB_SIZE);

3
index.d.ts vendored
View File

@ -68,7 +68,8 @@ declare module "react-native-wechat-lib" {
} }
export interface ShareImageMetadata { export interface ShareImageMetadata {
imageUrl: string, imageUrl: string,
scene?: WXScene scene?: WXScene,
maxWidth?: number
} }
export interface ShareMusicMetadata { export interface ShareMusicMetadata {
musicUrl: string, musicUrl: string,