|
...
|
...
|
@@ -1604,15 +1604,19 @@ class Utils { |
|
|
|
Loading.showLoading(context);
|
|
|
|
|
|
|
|
Uint8List result = list;
|
|
|
|
Uint8List resultnew;
|
|
|
|
if (needResize) {
|
|
|
|
result = await resizeImage(list); //重置图片尺寸
|
|
|
|
result = await resizeImage(list); //重置图片尺寸,裁剪5:4尺寸
|
|
|
|
if (result == null) {
|
|
|
|
//取消重置尺寸
|
|
|
|
Loading.hideLoading(context);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
resultnew = await realComporessList(result); //压缩图片
|
|
|
|
} else {
|
|
|
|
resultnew = await compressImage(list);
|
|
|
|
debugPrint("压缩后图片的大小:${size(result.length)}");
|
|
|
|
}
|
|
|
|
Uint8List resultnew = await realComporessList(result); //压缩图片
|
|
|
|
|
|
|
|
Loading.hideLoading(context);
|
|
|
|
return resultnew;
|
|
...
|
...
|
@@ -1624,43 +1628,121 @@ class Utils { |
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 图片压缩 Uint8List -> Uint8List
|
|
|
|
static Future<Uint8List> testComporessList(
|
|
|
|
BuildContext context, String url) async {
|
|
|
|
///本地选择 图片压缩 Uint8List -> Uint8List
|
|
|
|
static Future<Uint8List> testFromFileComporessList5R4(
|
|
|
|
BuildContext context,
|
|
|
|
Uint8List list,
|
|
|
|
) async {
|
|
|
|
Loading.showLoading(context);
|
|
|
|
Uint8List list = await downloadFile(url); //下载图片
|
|
|
|
Uint8List result = await resizeImage(list); //重置图片尺寸
|
|
|
|
|
|
|
|
Uint8List result = list;
|
|
|
|
|
|
|
|
result = await resizeImage(list); //重置图片尺寸,裁剪5:4尺寸
|
|
|
|
if (result == null) {
|
|
|
|
//取消重置尺寸
|
|
|
|
Loading.hideLoading(context);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Uint8List resultnew = await realComporessList(result); //压缩图片
|
|
|
|
|
|
|
|
Uint8List resultnew = await compressImage(list);
|
|
|
|
debugPrint("压缩后图片的大小:${size(result.length)}");
|
|
|
|
|
|
|
|
Loading.hideLoading(context);
|
|
|
|
return resultnew;
|
|
|
|
// if (result.length < 128 * 1024) {
|
|
|
|
// return result;
|
|
|
|
// } else {
|
|
|
|
// result = await realComporessList(result);
|
|
|
|
// return result;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 图片压缩 Uint8List -> Uint8List
|
|
|
|
// static Future<Uint8List> testComporessList(
|
|
|
|
// BuildContext context, String url) async {
|
|
|
|
// Loading.showLoading(context);
|
|
|
|
// Uint8List list = await downloadFile(url); //下载图片
|
|
|
|
// Uint8List result = await resizeImage(list); //重置图片尺寸
|
|
|
|
// if (result == null) {
|
|
|
|
// //取消重置尺寸
|
|
|
|
// Loading.hideLoading(context);
|
|
|
|
// return null;
|
|
|
|
// }
|
|
|
|
// Uint8List resultnew = await realComporessList(result); //压缩图片
|
|
|
|
|
|
|
|
// Loading.hideLoading(context);
|
|
|
|
// return resultnew;
|
|
|
|
// // if (result.length < 128 * 1024) {
|
|
|
|
// // return result;
|
|
|
|
// // } else {
|
|
|
|
// // result = await realComporessList(result);
|
|
|
|
// // return result;
|
|
|
|
// // }
|
|
|
|
// }
|
|
|
|
|
|
|
|
static realComporessList(Uint8List list) async {
|
|
|
|
debugPrint("压缩前图片的大小:${size(list.length)}");
|
|
|
|
int minHeight = 457; // 指定最小分辨率的高度
|
|
|
|
int minWidth = 572; // 指定最小分辨率的宽度
|
|
|
|
int quality = imageQuality(list.length);
|
|
|
|
|
|
|
|
print(size(list.length));
|
|
|
|
Uint8List result = await FlutterImageCompress.compressWithList(
|
|
|
|
list,
|
|
|
|
minWidth: minWidth,
|
|
|
|
minHeight: minHeight,
|
|
|
|
quality: quality,
|
|
|
|
);
|
|
|
|
debugPrint("压缩后图片的大小:${size(result.length)}");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 异步方法,压缩图片到指定大小
|
|
|
|
static Future<Uint8List> compressImage(Uint8List imageBytes,
|
|
|
|
{int quality = 90}) async {
|
|
|
|
try {
|
|
|
|
print("图片初始大小:${imageBytes.length / 1024 / 1024}MB");
|
|
|
|
|
|
|
|
// 控制压缩后图片大小,不得超过0.5MB
|
|
|
|
// int quality = 90;
|
|
|
|
while (imageBytes.length > 1024 * 1024 * 2 && quality > 0) {
|
|
|
|
imageBytes = await FlutterImageCompress.compressWithList(
|
|
|
|
imageBytes,
|
|
|
|
quality: quality,
|
|
|
|
);
|
|
|
|
print('压缩后大小: ${imageBytes.length / 1024 / 1024}MB');
|
|
|
|
quality -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageBytes;
|
|
|
|
} catch (e) {
|
|
|
|
// 处理异常情况
|
|
|
|
print('压缩图片出错:$e');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 异步方法,压缩图片到指定大小
|
|
|
|
static Future<File> compressImageToFile(String path,
|
|
|
|
{int quality = 90}) async {
|
|
|
|
try {
|
|
|
|
File image = File(path);
|
|
|
|
Uint8List imageBytes = await image.readAsBytes();
|
|
|
|
print("图片初始大小:${imageBytes.length / 1024 / 1024}MB");
|
|
|
|
|
|
|
|
// 控制压缩后图片大小,不得超过0.5MB
|
|
|
|
// int quality = 90;
|
|
|
|
while (imageBytes.length > 1024 * 1024 * 2 && quality > 0) {
|
|
|
|
imageBytes = await FlutterImageCompress.compressWithList(
|
|
|
|
imageBytes,
|
|
|
|
quality: quality,
|
|
|
|
);
|
|
|
|
print('压缩后大小: ${imageBytes.length / 1024 / 1024}MB');
|
|
|
|
quality -= 2;
|
|
|
|
}
|
|
|
|
String imgPath = await saveImage(imageBytes);
|
|
|
|
return File(imgPath);
|
|
|
|
} catch (e) {
|
|
|
|
// 处理异常情况
|
|
|
|
print('压缩图片出错:$e');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 根据传入的图片字节长度,返回指定的图片质量
|
|
|
|
static int imageQuality(int length) {
|
|
|
|
debugPrint("压缩前图片的大小:${size(length)}");
|
...
|
...
|
|