|
|
|
package com.wxwork.flutter_wxwork;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
import io.flutter.embedding.engine.plugins.FlutterPlugin;
|
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
|
|
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
|
|
|
import io.flutter.plugin.common.MethodChannel.Result;
|
|
|
|
import com.tencent.wework.api.model.WWMediaText;
|
|
|
|
|
|
|
|
// package com.example.flutter_wxwork;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.tencent.wework.api.IWWAPI;
|
|
|
|
import com.tencent.wework.api.IWWAPIEventHandler;
|
|
|
|
import com.tencent.wework.api.WWAPIFactory;
|
|
|
|
import com.tencent.wework.api.model.BaseMessage;
|
|
|
|
import com.tencent.wework.api.model.WWAuthMessage;
|
|
|
|
import com.tencent.wework.api.model.WWMediaMiniProgram;
|
|
|
|
import com.tencent.wework.api.model.WWSimpleRespMessage;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import com.wxwork.flutter_wxwork.LoadImageUtil;
|
|
|
|
import com.wxwork.flutter_wxwork.NetWorkUtils;
|
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
|
|
|
import java.net.URLConnection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FlutterWxworkPlugin
|
|
|
|
*/
|
|
|
|
public class FlutterWxworkPlugin implements FlutterPlugin, MethodCallHandler {
|
|
|
|
/// The MethodChannel that will the communication between Flutter and native Android
|
|
|
|
///
|
|
|
|
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
|
|
|
|
/// when the Flutter Engine is detached from the Activity
|
|
|
|
private MethodChannel channel;
|
|
|
|
|
|
|
|
private String APPID;
|
|
|
|
private String AGENTID;
|
|
|
|
private String SCHEMA;
|
|
|
|
|
|
|
|
private IWWAPI iwwapi;
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
private static final int SHARE_MINI_PROGRAM_THUMB_LENGTH = 120 ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
|
|
|
|
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_wxwork");
|
|
|
|
channel.setMethodCallHandler(this);
|
|
|
|
context = flutterPluginBinding.getApplicationContext();
|
|
|
|
iwwapi = WWAPIFactory.createWWAPI(context);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Map<String, String> arguments = (Map<String, String>) call.arguments;
|
|
|
|
|
|
|
|
if (call.method.equals("isAppInstalled")) {
|
|
|
|
boolean installed = iwwapi.isWWAppInstalled();
|
|
|
|
// if (installed) {
|
|
|
|
result.success(installed);
|
|
|
|
// } else {
|
|
|
|
// result.success("0");
|
|
|
|
// }
|
|
|
|
} else if (call.method.equals("register")) {
|
|
|
|
APPID = arguments.get("corpId");
|
|
|
|
SCHEMA = arguments.get("scheme");
|
|
|
|
AGENTID = arguments.get("agentId");
|
|
|
|
boolean hasRegister = iwwapi.registerApp(SCHEMA);
|
|
|
|
// if (hasRegister) {
|
|
|
|
result.success(hasRegister);
|
|
|
|
// } else {
|
|
|
|
// result.success("0");
|
|
|
|
// }
|
|
|
|
} else if (call.method.equals("auth")) {
|
|
|
|
String state = arguments.get("state");
|
|
|
|
|
|
|
|
final WWAuthMessage.Req req = new WWAuthMessage.Req();
|
|
|
|
req.sch = SCHEMA;
|
|
|
|
req.appId = APPID;
|
|
|
|
req.agentId = AGENTID;
|
|
|
|
req.state = state;
|
|
|
|
|
|
|
|
// Toast.makeText(context, "sch:" + req.sch,
|
|
|
|
// Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
iwwapi.sendMessage(req, new IWWAPIEventHandler() {
|
|
|
|
@Override
|
|
|
|
public void handleResp(BaseMessage resp) {
|
|
|
|
if (resp instanceof WWAuthMessage.Resp) {
|
|
|
|
WWAuthMessage.Resp rsp = (WWAuthMessage.Resp) resp;
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
map.put("code", rsp.code);
|
|
|
|
map.put("errCode", String.valueOf(rsp.errCode));
|
|
|
|
map.put("state", String.valueOf(rsp.state));
|
|
|
|
result.success(map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}else if (call.method.equals("shareImageNetWork")) {
|
|
|
|
// APPID = arguments.get("corpId");
|
|
|
|
// SCHEMA = arguments.get("scheme");
|
|
|
|
// AGENTID = arguments.get("agentId");
|
|
|
|
// boolean hasRegister = iwwapi.registerApp(SCHEMA);
|
|
|
|
// // if (hasRegister) {
|
|
|
|
// result.success(hasRegister);
|
|
|
|
// // } else {
|
|
|
|
// // result.success("0");
|
|
|
|
// // }
|
|
|
|
} else if (call.method.equals("shareImagePath")) {
|
|
|
|
}else if (call.method.equals("shareMiniProgram")) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WWMediaMiniProgram miniProgram = new WWMediaMiniProgram();
|
|
|
|
miniProgram.appPkg = "com.naji.one_key";
|
|
|
|
miniProgram.appName = "周到安排商家";//app name of this android pkg
|
|
|
|
miniProgram.appId = APPID; //企业唯一标识。创建企业后显示在,我的企业 CorpID字段
|
|
|
|
miniProgram.agentId = AGENTID; //应用唯一标识。显示在具体应用下的 AgentId字段
|
|
|
|
miniProgram.schema = SCHEMA;
|
|
|
|
|
|
|
|
miniProgram.username = arguments.get("userName"); //必须是应用关联的小程序,注意要有@app后缀
|
|
|
|
miniProgram.description = arguments.get("description");
|
|
|
|
miniProgram.path = arguments.get("path");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//得到图片
|
|
|
|
DownloadImageTask task = new DownloadImageTask(new DownloadImageTask.OnBitmapDownloadListener() {
|
|
|
|
@Override
|
|
|
|
public void onBitmapDownloaded(Bitmap bitmap) {
|
|
|
|
|
|
|
|
|
|
|
|
//缩略图建议不超过32kb
|
|
|
|
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 500, 400, true);
|
|
|
|
bitmap.recycle();
|
|
|
|
miniProgram.hdImageData = bmpToByteArray(thumbBmp, true);
|
|
|
|
// 在这里处理下载到的Bitmap对象
|
|
|
|
// miniProgram.hdImageData = getBytesFromBitmap(bitmap);
|
|
|
|
miniProgram.title = arguments.get("title");
|
|
|
|
// 数据加载完毕后隐藏加载动画
|
|
|
|
result.success(true);
|
|
|
|
iwwapi.sendMessage(miniProgram, new IWWAPIEventHandler() {
|
|
|
|
@Override
|
|
|
|
public void handleResp(BaseMessage resp) {
|
|
|
|
|
|
|
|
if(resp instanceof WWSimpleRespMessage){
|
|
|
|
WWSimpleRespMessage rsp = (WWSimpleRespMessage)resp;
|
|
|
|
// String t = "";
|
|
|
|
// Toast.makeText(MainActivity.this,
|
|
|
|
// t = "发小程序," + rsp.errCode + "," + rsp.errMsg,
|
|
|
|
// Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
map.put("code", String.valueOf(rsp.errCode));
|
|
|
|
map.put("errCode", String.valueOf(rsp.errMsg));
|
|
|
|
|
|
|
|
|
|
|
|
// result.success(map);
|
|
|
|
}else{
|
|
|
|
// result.success(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Log.i("ExampleClass", "Value: " + arguments.get("hdImageData"));
|
|
|
|
task.execute(arguments.get("hdImageData"));
|
|
|
|
// LoadImageUtil loadImageUtil = new LoadImageUtil(context);
|
|
|
|
// Bitmap bitmap = loadImageUtil.loadBitmapByImgUrl(arguments.get("hdImageData"), new LoadImageUtil.CallBack() {
|
|
|
|
// @Override
|
|
|
|
// public void callBack(String imgUrl, Bitmap bitmap) {
|
|
|
|
// if(bitmap != null){
|
|
|
|
//// ImageView img = (ImageView) parent.findViewWithTag(imgUrl);
|
|
|
|
//// img.setImageBitmap(bitmap);
|
|
|
|
// miniProgram.hdImageData = getBytesFromBitmap(bitmap);
|
|
|
|
// miniProgram.title = arguments.get("title");
|
|
|
|
// iwwapi.sendMessage(miniProgram, new IWWAPIEventHandler() {
|
|
|
|
// @Override
|
|
|
|
// public void handleResp(BaseMessage resp) {
|
|
|
|
// if(resp instanceof WWSimpleRespMessage){
|
|
|
|
// WWSimpleRespMessage rsp = (WWSimpleRespMessage)resp;
|
|
|
|
//// String t = "";
|
|
|
|
//// Toast.makeText(MainActivity.this,
|
|
|
|
//// t = "发小程序," + rsp.errCode + "," + rsp.errMsg,
|
|
|
|
//// Toast.LENGTH_LONG).show();
|
|
|
|
//
|
|
|
|
// Map<String, String> map = new HashMap<>();
|
|
|
|
// map.put("code", String.valueOf(rsp.errCode));
|
|
|
|
// map.put("errCode", String.valueOf(rsp.errMsg));
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// result.success(map);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// if(bitmap != null){//如果图片在缓存中,直接获取展示
|
|
|
|
// miniProgram.hdImageData = getBytesFromBitmap(bitmap);
|
|
|
|
// miniProgram.title = arguments.get("title");
|
|
|
|
// iwwapi.sendMessage(miniProgram, new IWWAPIEventHandler() {
|
|
|
|
// @Override
|
|
|
|
// public void handleResp(BaseMessage resp) {
|
|
|
|
// if(resp instanceof WWSimpleRespMessage){
|
|
|
|
// WWSimpleRespMessage rsp = (WWSimpleRespMessage)resp;
|
|
|
|
//// String t = "";
|
|
|
|
//// Toast.makeText(MainActivity.this,
|
|
|
|
//// t = "发小程序," + rsp.errCode + "," + rsp.errMsg,
|
|
|
|
//// Toast.LENGTH_LONG).show();
|
|
|
|
//
|
|
|
|
// Map<String, String> map = new HashMap<>();
|
|
|
|
// map.put("code", String.valueOf(rsp.errCode));
|
|
|
|
// map.put("errCode", String.valueOf(rsp.errMsg));
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// result.success(map);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Bitmap bitmap = getBitmapFromURL(arguments.get("hdImageData"));
|
|
|
|
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
// bitmap.compress(Bitmap.CompressFormat.JPEG, 0, stream);
|
|
|
|
// byte[] byteArray = stream.toByteArray();
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}else if (call.method.equals("shareText")) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WWMediaText txt = new WWMediaText( arguments.get("text")
|
|
|
|
);
|
|
|
|
txt.appPkg = "com.naji.one_key";
|
|
|
|
txt.appName = "周到安排商家" ;
|
|
|
|
txt.appId = APPID; //企业唯一标识。创建企业后显示在,我的企业 CorpID字段
|
|
|
|
txt.agentId = AGENTID; //应用唯一标识。显示在具体应用下的 AgentId字段
|
|
|
|
iwwapi.sendMessage(txt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
// result(FlutterMethodNotImplemented);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 转换图片格式为字节数组
|
|
|
|
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
|
|
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
|
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
|
|
|
|
if (needRecycle) {
|
|
|
|
bmp.recycle();
|
|
|
|
}
|
|
|
|
byte[] result = output.toByteArray();
|
|
|
|
try {
|
|
|
|
output.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Bitmap compressBitmap(String imgPath, int targetWidth, int targetHeight, int quality, Bitmap.CompressFormat compressFormat) {
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
options.inJustDecodeBounds = true;
|
|
|
|
BitmapFactory.decodeFile(imgPath, options);
|
|
|
|
int actualHeight = options.outHeight;
|
|
|
|
int actualWidth = options.outWidth;
|
|
|
|
|
|
|
|
float ratio = actualWidth / (float) actualHeight;
|
|
|
|
options.inSampleSize = 1;
|
|
|
|
while ((actualWidth / 2) > targetWidth && (actualHeight / 2) > targetHeight) {
|
|
|
|
actualWidth /= 2;
|
|
|
|
actualHeight /= 2;
|
|
|
|
options.inSampleSize *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.inJustDecodeBounds = false;
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
|
|
|
|
|
|
|
|
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, (int) (targetWidth / ratio), true);
|
|
|
|
bitmap.recycle();
|
|
|
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
scaledBitmap.compress(compressFormat, quality, byteArrayOutputStream);
|
|
|
|
scaledBitmap.recycle();
|
|
|
|
|
|
|
|
return BitmapFactory.decodeByteArray(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
|
|
|
|
// Bitmap compressedImage = compressBitmap(bitmap,100);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
|
|
|
|
bitmap.copyPixelsToBuffer(byteBuffer);
|
|
|
|
|
|
|
|
return byteBuffer.array();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 压缩图片
|
|
|
|
*
|
|
|
|
* @param bitmap
|
|
|
|
* 被压缩的图片
|
|
|
|
* @param sizeLimit
|
|
|
|
* 大小限制
|
|
|
|
* @return
|
|
|
|
* 压缩后的图片
|
|
|
|
*/
|
|
|
|
private static Bitmap compressBitmap(Bitmap bitmap, long sizeLimit) {
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
int quality = 10;
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
|
|
|
|
|
|
|
|
// 循环判断压缩后图片是否超过限制大小
|
|
|
|
while(baos.toByteArray().length / 1024 > sizeLimit) {
|
|
|
|
// 清空baos
|
|
|
|
baos.reset();
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
|
|
|
|
quality -= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap newBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), null, null);
|
|
|
|
|
|
|
|
return newBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// public static Bitmap compressBitmapToSize(Bitmap bitmap, int maxSizeKB) {
|
|
|
|
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
// bitmap.compress(getBitmapCompressFormat(bitmap), 100, baos); // 假设压缩格式为JPEG
|
|
|
|
// int options = 100;
|
|
|
|
//
|
|
|
|
// int bytes = baos.toByteArray().length/1024;
|
|
|
|
////
|
|
|
|
// // 如果Bitmap的大小已经小于或等于指定的大小,则不需要进一步压缩
|
|
|
|
// while (bytes > maxSizeKB ) {
|
|
|
|
// // 每次减少质量,然后重新压缩
|
|
|
|
// options -= 5; // 降低质量
|
|
|
|
// baos.reset(); // 重置baos来清除数据
|
|
|
|
// bitmap.compress(getBitmapCompressFormat(bitmap), options, baos);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// bytes = baos.toByteArray().length/1024;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//// while (baos.toByteArray().length / 1024 > maxSizeKB && options != 10) {
|
|
|
|
//// baos.reset(); // 清空baos以复用
|
|
|
|
//// options -= 10; // 每次减少10
|
|
|
|
//// bitmap.compress(getBitmapCompressFormat(bitmap), options, baos);
|
|
|
|
//// }
|
|
|
|
// ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
|
|
|
|
// Bitmap compressedBitmap = BitmapFactory.decodeStream(isBm, null, null);
|
|
|
|
// // 释放资源
|
|
|
|
// baos.reset();
|
|
|
|
// try {
|
|
|
|
// baos.close();
|
|
|
|
// isBm.close();
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// return compressedBitmap;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public static Bitmap compressImage(Bitmap bitmap){
|
|
|
|
// int height = (int) ( bitmap.getHeight() * (512.0 / bitmap.getWidth()) );
|
|
|
|
// bitmap = Bitmap.createScaledBitmap(bitmap, 512, height, true);
|
|
|
|
// return bitmap;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// public static Bitmap compressBitmapToSize(Bitmap image, int maxSizeKB) {
|
|
|
|
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
// // 计算压缩比例
|
|
|
|
// int quality = 100;
|
|
|
|
// image.compress(Bitmap.CompressFormat.JPEG, quality, stream);
|
|
|
|
// // 如果图片大小大于指定大小,则以压缩质量为基准重复压缩
|
|
|
|
// while ((stream.toByteArray().length / 1024) > maxSizeKB && quality > 0) {
|
|
|
|
// stream.reset(); // 重置流以复用
|
|
|
|
// quality -= 5; // 每次减少5%的质量
|
|
|
|
// image.compress(getBitmapCompressFormat(image), quality, stream);
|
|
|
|
// }
|
|
|
|
// // 将流转换为字节
|
|
|
|
// byte[] imageBytes = stream.toByteArray();
|
|
|
|
// // 将字节转换为Bitmap
|
|
|
|
// Bitmap compressedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
|
|
|
|
// // 释放资源
|
|
|
|
//
|
|
|
|
//// 关闭流
|
|
|
|
// try {
|
|
|
|
// stream.close();
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// return compressedImage;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// public static Bitmap compressBitmapToSize(Bitmap bitmap, int maxSizeKB) {
|
|
|
|
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
// int quality = 100;
|
|
|
|
// int bytes = baos.toByteArray().length;
|
|
|
|
//
|
|
|
|
// // 如果Bitmap的大小已经小于或等于指定的大小,则不需要进一步压缩
|
|
|
|
// while (bytes > maxSizeKB ) {
|
|
|
|
// // 每次减少质量,然后重新压缩
|
|
|
|
// quality -= 5; // 降低质量
|
|
|
|
// baos.reset(); // 重置baos来清除数据
|
|
|
|
// bitmap.compress(getBitmapCompressFormat(bitmap), quality, baos);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// bytes = baos.toByteArray().length;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // 将压缩后的数据转换回Bitmap对象
|
|
|
|
// ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
|
|
|
|
// Bitmap compressedBitmap = BitmapFactory.decodeStream(isBm, null, null);
|
|
|
|
// // 关闭流
|
|
|
|
// try {
|
|
|
|
// baos.close();
|
|
|
|
// isBm.close();
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// return compressedBitmap;
|
|
|
|
// }
|
|
|
|
|
|
|
|
public static Bitmap.CompressFormat getBitmapCompressFormat(Bitmap bitmap) {
|
|
|
|
Bitmap.Config config = bitmap.getConfig();
|
|
|
|
if (config == null) {
|
|
|
|
return Bitmap.CompressFormat.PNG;
|
|
|
|
}
|
|
|
|
switch (config) {
|
|
|
|
case RGB_565:
|
|
|
|
return Bitmap.CompressFormat.JPEG;
|
|
|
|
case ALPHA_8:
|
|
|
|
case ARGB_4444:
|
|
|
|
case ARGB_8888:
|
|
|
|
return Bitmap.CompressFormat.PNG;
|
|
|
|
default:
|
|
|
|
return Bitmap.CompressFormat.WEBP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public static Bitmap getBitmapFromURL(String imageUrl) {
|
|
|
|
// HttpURLConnection connection = null;
|
|
|
|
// try {
|
|
|
|
// URL url = new URL(imageUrl);
|
|
|
|
// connection = (HttpURLConnection) url.openConnection();
|
|
|
|
// connection.setDoInput(true);
|
|
|
|
// connection.connect();
|
|
|
|
// BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
|
|
|
|
// Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
|
|
|
|
// inputStream.close();
|
|
|
|
// return myBitmap;
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// return null;
|
|
|
|
// } finally {
|
|
|
|
// if (connection != null) {
|
|
|
|
// connection.disconnect();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
|
|
|
|
channel.setMethodCallHandler(null);
|
|
|
|
}
|
|
|
|
} |
|
|
\ No newline at end of file |