Commit c52d485cc45581618381925ea9c43b89ee5bfbb0

Authored by wdn127929
1 parent ed7b49e1

图片压缩调整

... ... @@ -370,9 +370,9 @@ class _BookInfoPageState extends State<BookInfoPage> {
if (needResize) {
File yourFile = File(pickedFile.path);
Uint8List afterImg = await yourFile.readAsBytes();
Uint8List newImg = await Utils.testFromFileComporessList(
Uint8List newImg = await Utils.testFromFileComporessList5R4(
context, afterImg,
needResize: true);
);
if (newImg != null) {
path = await Utils.saveImage(newImg);
if (path == null) {
... ...
... ... @@ -31,6 +31,7 @@ import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import './dispose.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:flutter_native_image/flutter_native_image.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
typedef void OnPickImageCallback(
double maxWidth, double maxHeight, int quality);
... ... @@ -104,10 +105,14 @@ class _ImgPickerWidgetState extends DisposableState<ImgPickerWidget> {
Uint8List newImg = await Utils.testFromFileComporessList(context, afterImg);
String path = await Utils.saveImage(newImg);
if (path == null) {
File compressedFile = await FlutterNativeImage.compressImage(
yourFile.path,
quality: 70,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// // File compressedFile = await FlutterImageCompress.compressAndGetFile(
// yourFile.path,
// quality: 70,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(yourFile.path, quality: 70);
path = compressedFile.path;
}
... ... @@ -245,10 +250,12 @@ class _ImgPickerWidgetState extends DisposableState<ImgPickerWidget> {
String path = await Utils.saveImage(newImg);
// File compressedFile;
if (path == null) {
File compressedFile = await FlutterNativeImage.compressImage(
file.path,
quality: 50,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// file.path,
// quality: 50,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(file.path, quality: 70);
path = compressedFile.path;
}
// File compressedFile = await FlutterNativeImage.compressImage(
... ...
... ... @@ -105,10 +105,13 @@ class _ImgPickerFWidgetState extends DisposableState<ImgPickerFWidget> {
Future _getFromPictureOrCamera(imageSource) async {
final pickedFile = await picker.pickImage(source: imageSource);
if (pickedFile != null) {
File compressedFile = await FlutterNativeImage.compressImage(
pickedFile.path,
quality: 50,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// pickedFile.path,
// quality: 70,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(pickedFile.path, quality: 70);
setState(() {
if (pickedFile != null) {
UploadImgModel imgModel = UploadImgModel();
... ... @@ -250,10 +253,12 @@ class _ImgPickerFWidgetState extends DisposableState<ImgPickerFWidget> {
} else {
for (int i = 0; i < imgList.length; i++) {
var file = await imgList[i].file;
File compressedFile = await FlutterNativeImage.compressImage(
file.absolute.path,
quality: 50,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// file.absolute.path,
// quality: 50,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(file.absolute.path, quality: 70);
setState(() {
UploadImgModel imgModel = UploadImgModel();
imgModel.file = compressedFile;
... ...
... ... @@ -406,10 +406,12 @@ class _BaseInfoPageSimState extends State<BaseInfoPageSim> {
String title, UploadImgModel imgModel, imageSource) async {
final pickedFile = await picker.pickImage(source: imageSource);
if (pickedFile != null) {
File compressedFile = await FlutterNativeImage.compressImage(
pickedFile.path,
quality: 70,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// pickedFile.path,
// quality: 70,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(pickedFile.path, quality: 70);
setState(() {
if (pickedFile != null) {
imgModel.file = File(pickedFile.path);
... ...
... ... @@ -308,10 +308,12 @@ class _BookInfoPageSimState extends State<BookInfoPageSim> {
if (newImg != null) {
path = await Utils.saveImage(newImg);
if (path == null) {
File compressedFile = await FlutterNativeImage.compressImage(
pickedFile.path,
quality: 70,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// pickedFile.path,
// quality: 70,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(pickedFile.path, quality: 70);
path = compressedFile.path;
}
... ... @@ -347,10 +349,12 @@ class _BookInfoPageSimState extends State<BookInfoPageSim> {
}
} else {
if (path == null) {
File compressedFile = await FlutterNativeImage.compressImage(
pickedFile.path,
quality: 70,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// pickedFile.path,
// quality: 70,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(pickedFile.path, quality: 70);
path = compressedFile.path;
}
... ...
... ... @@ -262,10 +262,12 @@ class _MarkingInteractionPageSimState extends State<MarkingInteractionPageSim> {
Future _getFromPictureOrCamera(UploadImgModel imgModel, imageSource) async {
final pickedFile = await picker.pickImage(source: imageSource);
if (pickedFile != null) {
File compressedFile = await FlutterNativeImage.compressImage(
pickedFile.path,
quality: 50,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// pickedFile.path,
// quality: 50,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(pickedFile.path, quality: 70);
setState(() {
if (pickedFile != null) {
imgModel.file = File(pickedFile.path);
... ...
... ... @@ -749,10 +749,12 @@ class _StoreBaseInfoPageSimState extends State<StoreBaseInfoPageSim> {
if (newImg != null) {
String path = await Utils.saveImage(newImg);
if (path == null) {
File compressedFile = await FlutterNativeImage.compressImage(
pickedFile.path,
quality: 70,
percentage: 50);
// File compressedFile = await FlutterNativeImage.compressImage(
// pickedFile.path,
// quality: 70,
// percentage: 50);
File compressedFile =
await Utils.compressImageToFile(pickedFile.path, quality: 70);
path = compressedFile.path;
}
//更新本地存储,上传中展示
... ...
... ... @@ -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)}");
... ...
... ... @@ -621,7 +621,42 @@ packages:
name: flutter_image_compress
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.3"
version: "2.3.0"
flutter_image_compress_common:
dependency: transitive
description:
name: flutter_image_compress_common
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.5"
flutter_image_compress_macos:
dependency: transitive
description:
name: flutter_image_compress_macos
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.2"
flutter_image_compress_ohos:
dependency: transitive
description:
name: flutter_image_compress_ohos
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.0.3"
flutter_image_compress_platform_interface:
dependency: transitive
description:
name: flutter_image_compress_platform_interface
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.5"
flutter_image_compress_web:
dependency: transitive
description:
name: flutter_image_compress_web
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.4+1"
flutter_inappwebview:
dependency: "direct main"
description:
... ...
... ... @@ -225,7 +225,7 @@ dependencies:
extended_text_field: ^10.1.1
# image_gallery_saver: ^1.7.1
image_gallery_saver: ^2.0.3
flutter_image_compress: ^1.1.3
flutter_image_compress: ^2.3.0
image: ^4.0.15
image_cropper: ^3.0.1
# image_cropper: ^4.0.1
... ...