【发布时间】:2018-12-28 16:49:53
【问题描述】:
我从服务器获取字符串变量中的图像,我需要在移动设备中减少并重新上传。我有下一个功能,但我不知道为什么要花这么多时间,我真的不知道它是否在某个时候被阻止/冻结,或者这个过程需要这么多时间。
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
public static Bitmap decodeBase64(String input)
{
byte[] decodedBytes = Base64.decode(input, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
public static String compressImage(String base64ImageData){
long SIZE_1_5_MB = 1572864;
int QUALITY = 100;
Bitmap decodedByte = decodeBase64(base64ImageData);
StringBuilder sb = new StringBuilder();
do{
sb.setLength(0);
sb = new StringBuilder();
String eBase64 = encodeToBase64(decodedByte, Bitmap.CompressFormat.JPEG, QUALITY);
sb.append(eBase64);
Log.e("IMAGE", "QUALITY: " + QUALITY + " SIZE: " + sb.length());
QUALITY = QUALITY - 5;
}while (sb.length() > SIZE_1_5_MB);
return sb.toString();
}
我想将照片缩小到小于 1.5 mb
【问题讨论】: