Commit c52d485cc45581618381925ea9c43b89ee5bfbb0

Authored by wdn127929
1 parent ed7b49e1

图片压缩调整

@@ -370,9 +370,9 @@ class _BookInfoPageState extends State<BookInfoPage> { @@ -370,9 +370,9 @@ class _BookInfoPageState extends State<BookInfoPage> {
370 if (needResize) { 370 if (needResize) {
371 File yourFile = File(pickedFile.path); 371 File yourFile = File(pickedFile.path);
372 Uint8List afterImg = await yourFile.readAsBytes(); 372 Uint8List afterImg = await yourFile.readAsBytes();
373 - Uint8List newImg = await Utils.testFromFileComporessList( 373 + Uint8List newImg = await Utils.testFromFileComporessList5R4(
374 context, afterImg, 374 context, afterImg,
375 - needResize: true); 375 + );
376 if (newImg != null) { 376 if (newImg != null) {
377 path = await Utils.saveImage(newImg); 377 path = await Utils.saveImage(newImg);
378 if (path == null) { 378 if (path == null) {
@@ -31,6 +31,7 @@ import 'package:wechat_assets_picker/wechat_assets_picker.dart'; @@ -31,6 +31,7 @@ import 'package:wechat_assets_picker/wechat_assets_picker.dart';
31 import './dispose.dart'; 31 import './dispose.dart';
32 import 'package:photo_manager/photo_manager.dart'; 32 import 'package:photo_manager/photo_manager.dart';
33 import 'package:flutter_native_image/flutter_native_image.dart'; 33 import 'package:flutter_native_image/flutter_native_image.dart';
  34 +import 'package:flutter_image_compress/flutter_image_compress.dart';
34 35
35 typedef void OnPickImageCallback( 36 typedef void OnPickImageCallback(
36 double maxWidth, double maxHeight, int quality); 37 double maxWidth, double maxHeight, int quality);
@@ -104,10 +105,14 @@ class _ImgPickerWidgetState extends DisposableState<ImgPickerWidget> { @@ -104,10 +105,14 @@ class _ImgPickerWidgetState extends DisposableState<ImgPickerWidget> {
104 Uint8List newImg = await Utils.testFromFileComporessList(context, afterImg); 105 Uint8List newImg = await Utils.testFromFileComporessList(context, afterImg);
105 String path = await Utils.saveImage(newImg); 106 String path = await Utils.saveImage(newImg);
106 if (path == null) { 107 if (path == null) {
107 - File compressedFile = await FlutterNativeImage.compressImage(  
108 - yourFile.path,  
109 - quality: 70,  
110 - percentage: 50); 108 + // File compressedFile = await FlutterNativeImage.compressImage(
  109 + // // File compressedFile = await FlutterImageCompress.compressAndGetFile(
  110 + // yourFile.path,
  111 +
  112 + // quality: 70,
  113 + // percentage: 50);
  114 + File compressedFile =
  115 + await Utils.compressImageToFile(yourFile.path, quality: 70);
111 path = compressedFile.path; 116 path = compressedFile.path;
112 } 117 }
113 118
@@ -245,10 +250,12 @@ class _ImgPickerWidgetState extends DisposableState<ImgPickerWidget> { @@ -245,10 +250,12 @@ class _ImgPickerWidgetState extends DisposableState<ImgPickerWidget> {
245 String path = await Utils.saveImage(newImg); 250 String path = await Utils.saveImage(newImg);
246 // File compressedFile; 251 // File compressedFile;
247 if (path == null) { 252 if (path == null) {
248 - File compressedFile = await FlutterNativeImage.compressImage(  
249 - file.path,  
250 - quality: 50,  
251 - percentage: 50); 253 + // File compressedFile = await FlutterNativeImage.compressImage(
  254 + // file.path,
  255 + // quality: 50,
  256 + // percentage: 50);
  257 + File compressedFile =
  258 + await Utils.compressImageToFile(file.path, quality: 70);
252 path = compressedFile.path; 259 path = compressedFile.path;
253 } 260 }
254 // File compressedFile = await FlutterNativeImage.compressImage( 261 // File compressedFile = await FlutterNativeImage.compressImage(
@@ -105,10 +105,13 @@ class _ImgPickerFWidgetState extends DisposableState<ImgPickerFWidget> { @@ -105,10 +105,13 @@ class _ImgPickerFWidgetState extends DisposableState<ImgPickerFWidget> {
105 Future _getFromPictureOrCamera(imageSource) async { 105 Future _getFromPictureOrCamera(imageSource) async {
106 final pickedFile = await picker.pickImage(source: imageSource); 106 final pickedFile = await picker.pickImage(source: imageSource);
107 if (pickedFile != null) { 107 if (pickedFile != null) {
108 - File compressedFile = await FlutterNativeImage.compressImage(  
109 - pickedFile.path,  
110 - quality: 50,  
111 - percentage: 50); 108 + // File compressedFile = await FlutterNativeImage.compressImage(
  109 + // pickedFile.path,
  110 + // quality: 70,
  111 + // percentage: 50);
  112 +
  113 + File compressedFile =
  114 + await Utils.compressImageToFile(pickedFile.path, quality: 70);
112 setState(() { 115 setState(() {
113 if (pickedFile != null) { 116 if (pickedFile != null) {
114 UploadImgModel imgModel = UploadImgModel(); 117 UploadImgModel imgModel = UploadImgModel();
@@ -250,10 +253,12 @@ class _ImgPickerFWidgetState extends DisposableState<ImgPickerFWidget> { @@ -250,10 +253,12 @@ class _ImgPickerFWidgetState extends DisposableState<ImgPickerFWidget> {
250 } else { 253 } else {
251 for (int i = 0; i < imgList.length; i++) { 254 for (int i = 0; i < imgList.length; i++) {
252 var file = await imgList[i].file; 255 var file = await imgList[i].file;
253 - File compressedFile = await FlutterNativeImage.compressImage(  
254 - file.absolute.path,  
255 - quality: 50,  
256 - percentage: 50); 256 + // File compressedFile = await FlutterNativeImage.compressImage(
  257 + // file.absolute.path,
  258 + // quality: 50,
  259 + // percentage: 50);
  260 + File compressedFile =
  261 + await Utils.compressImageToFile(file.absolute.path, quality: 70);
257 setState(() { 262 setState(() {
258 UploadImgModel imgModel = UploadImgModel(); 263 UploadImgModel imgModel = UploadImgModel();
259 imgModel.file = compressedFile; 264 imgModel.file = compressedFile;
@@ -406,10 +406,12 @@ class _BaseInfoPageSimState extends State<BaseInfoPageSim> { @@ -406,10 +406,12 @@ class _BaseInfoPageSimState extends State<BaseInfoPageSim> {
406 String title, UploadImgModel imgModel, imageSource) async { 406 String title, UploadImgModel imgModel, imageSource) async {
407 final pickedFile = await picker.pickImage(source: imageSource); 407 final pickedFile = await picker.pickImage(source: imageSource);
408 if (pickedFile != null) { 408 if (pickedFile != null) {
409 - File compressedFile = await FlutterNativeImage.compressImage(  
410 - pickedFile.path,  
411 - quality: 70,  
412 - percentage: 50); 409 + // File compressedFile = await FlutterNativeImage.compressImage(
  410 + // pickedFile.path,
  411 + // quality: 70,
  412 + // percentage: 50);
  413 + File compressedFile =
  414 + await Utils.compressImageToFile(pickedFile.path, quality: 70);
413 setState(() { 415 setState(() {
414 if (pickedFile != null) { 416 if (pickedFile != null) {
415 imgModel.file = File(pickedFile.path); 417 imgModel.file = File(pickedFile.path);
@@ -308,10 +308,12 @@ class _BookInfoPageSimState extends State<BookInfoPageSim> { @@ -308,10 +308,12 @@ class _BookInfoPageSimState extends State<BookInfoPageSim> {
308 if (newImg != null) { 308 if (newImg != null) {
309 path = await Utils.saveImage(newImg); 309 path = await Utils.saveImage(newImg);
310 if (path == null) { 310 if (path == null) {
311 - File compressedFile = await FlutterNativeImage.compressImage(  
312 - pickedFile.path,  
313 - quality: 70,  
314 - percentage: 50); 311 + // File compressedFile = await FlutterNativeImage.compressImage(
  312 + // pickedFile.path,
  313 + // quality: 70,
  314 + // percentage: 50);
  315 + File compressedFile =
  316 + await Utils.compressImageToFile(pickedFile.path, quality: 70);
315 path = compressedFile.path; 317 path = compressedFile.path;
316 } 318 }
317 319
@@ -347,10 +349,12 @@ class _BookInfoPageSimState extends State<BookInfoPageSim> { @@ -347,10 +349,12 @@ class _BookInfoPageSimState extends State<BookInfoPageSim> {
347 } 349 }
348 } else { 350 } else {
349 if (path == null) { 351 if (path == null) {
350 - File compressedFile = await FlutterNativeImage.compressImage(  
351 - pickedFile.path,  
352 - quality: 70,  
353 - percentage: 50); 352 + // File compressedFile = await FlutterNativeImage.compressImage(
  353 + // pickedFile.path,
  354 + // quality: 70,
  355 + // percentage: 50);
  356 + File compressedFile =
  357 + await Utils.compressImageToFile(pickedFile.path, quality: 70);
354 path = compressedFile.path; 358 path = compressedFile.path;
355 } 359 }
356 360
@@ -262,10 +262,12 @@ class _MarkingInteractionPageSimState extends State<MarkingInteractionPageSim> { @@ -262,10 +262,12 @@ class _MarkingInteractionPageSimState extends State<MarkingInteractionPageSim> {
262 Future _getFromPictureOrCamera(UploadImgModel imgModel, imageSource) async { 262 Future _getFromPictureOrCamera(UploadImgModel imgModel, imageSource) async {
263 final pickedFile = await picker.pickImage(source: imageSource); 263 final pickedFile = await picker.pickImage(source: imageSource);
264 if (pickedFile != null) { 264 if (pickedFile != null) {
265 - File compressedFile = await FlutterNativeImage.compressImage(  
266 - pickedFile.path,  
267 - quality: 50,  
268 - percentage: 50); 265 + // File compressedFile = await FlutterNativeImage.compressImage(
  266 + // pickedFile.path,
  267 + // quality: 50,
  268 + // percentage: 50);
  269 + File compressedFile =
  270 + await Utils.compressImageToFile(pickedFile.path, quality: 70);
269 setState(() { 271 setState(() {
270 if (pickedFile != null) { 272 if (pickedFile != null) {
271 imgModel.file = File(pickedFile.path); 273 imgModel.file = File(pickedFile.path);
@@ -749,10 +749,12 @@ class _StoreBaseInfoPageSimState extends State<StoreBaseInfoPageSim> { @@ -749,10 +749,12 @@ class _StoreBaseInfoPageSimState extends State<StoreBaseInfoPageSim> {
749 if (newImg != null) { 749 if (newImg != null) {
750 String path = await Utils.saveImage(newImg); 750 String path = await Utils.saveImage(newImg);
751 if (path == null) { 751 if (path == null) {
752 - File compressedFile = await FlutterNativeImage.compressImage(  
753 - pickedFile.path,  
754 - quality: 70,  
755 - percentage: 50); 752 + // File compressedFile = await FlutterNativeImage.compressImage(
  753 + // pickedFile.path,
  754 + // quality: 70,
  755 + // percentage: 50);
  756 + File compressedFile =
  757 + await Utils.compressImageToFile(pickedFile.path, quality: 70);
756 path = compressedFile.path; 758 path = compressedFile.path;
757 } 759 }
758 //更新本地存储,上传中展示 760 //更新本地存储,上传中展示
@@ -1604,15 +1604,19 @@ class Utils { @@ -1604,15 +1604,19 @@ class Utils {
1604 Loading.showLoading(context); 1604 Loading.showLoading(context);
1605 1605
1606 Uint8List result = list; 1606 Uint8List result = list;
  1607 + Uint8List resultnew;
1607 if (needResize) { 1608 if (needResize) {
1608 - result = await resizeImage(list); //重置图片尺寸 1609 + result = await resizeImage(list); //重置图片尺寸,裁剪5:4尺寸
1609 if (result == null) { 1610 if (result == null) {
1610 //取消重置尺寸 1611 //取消重置尺寸
1611 Loading.hideLoading(context); 1612 Loading.hideLoading(context);
1612 return null; 1613 return null;
1613 } 1614 }
  1615 + resultnew = await realComporessList(result); //压缩图片
  1616 + } else {
  1617 + resultnew = await compressImage(list);
  1618 + debugPrint("压缩后图片的大小:${size(result.length)}");
1614 } 1619 }
1615 - Uint8List resultnew = await realComporessList(result); //压缩图片  
1616 1620
1617 Loading.hideLoading(context); 1621 Loading.hideLoading(context);
1618 return resultnew; 1622 return resultnew;
@@ -1624,43 +1628,121 @@ class Utils { @@ -1624,43 +1628,121 @@ class Utils {
1624 // } 1628 // }
1625 } 1629 }
1626 1630
1627 - /// 图片压缩 Uint8List -> Uint8List  
1628 - static Future<Uint8List> testComporessList(  
1629 - BuildContext context, String url) async { 1631 + ///本地选择 图片压缩 Uint8List -> Uint8List
  1632 + static Future<Uint8List> testFromFileComporessList5R4(
  1633 + BuildContext context,
  1634 + Uint8List list,
  1635 + ) async {
1630 Loading.showLoading(context); 1636 Loading.showLoading(context);
1631 - Uint8List list = await downloadFile(url); //下载图片  
1632 - Uint8List result = await resizeImage(list); //重置图片尺寸 1637 +
  1638 + Uint8List result = list;
  1639 +
  1640 + result = await resizeImage(list); //重置图片尺寸,裁剪5:4尺寸
1633 if (result == null) { 1641 if (result == null) {
1634 //取消重置尺寸 1642 //取消重置尺寸
1635 Loading.hideLoading(context); 1643 Loading.hideLoading(context);
1636 return null; 1644 return null;
1637 } 1645 }
1638 - Uint8List resultnew = await realComporessList(result); //压缩图片 1646 +
  1647 + Uint8List resultnew = await compressImage(list);
  1648 + debugPrint("压缩后图片的大小:${size(result.length)}");
1639 1649
1640 Loading.hideLoading(context); 1650 Loading.hideLoading(context);
1641 return resultnew; 1651 return resultnew;
1642 - // if (result.length < 128 * 1024) {  
1643 - // return result;  
1644 - // } else {  
1645 - // result = await realComporessList(result);  
1646 - // return result;  
1647 - // }  
1648 } 1652 }
1649 1653
  1654 + /// 图片压缩 Uint8List -> Uint8List
  1655 + // static Future<Uint8List> testComporessList(
  1656 + // BuildContext context, String url) async {
  1657 + // Loading.showLoading(context);
  1658 + // Uint8List list = await downloadFile(url); //下载图片
  1659 + // Uint8List result = await resizeImage(list); //重置图片尺寸
  1660 + // if (result == null) {
  1661 + // //取消重置尺寸
  1662 + // Loading.hideLoading(context);
  1663 + // return null;
  1664 + // }
  1665 + // Uint8List resultnew = await realComporessList(result); //压缩图片
  1666 +
  1667 + // Loading.hideLoading(context);
  1668 + // return resultnew;
  1669 + // // if (result.length < 128 * 1024) {
  1670 + // // return result;
  1671 + // // } else {
  1672 + // // result = await realComporessList(result);
  1673 + // // return result;
  1674 + // // }
  1675 + // }
  1676 +
1650 static realComporessList(Uint8List list) async { 1677 static realComporessList(Uint8List list) async {
  1678 + debugPrint("压缩前图片的大小:${size(list.length)}");
1651 int minHeight = 457; // 指定最小分辨率的高度 1679 int minHeight = 457; // 指定最小分辨率的高度
1652 int minWidth = 572; // 指定最小分辨率的宽度 1680 int minWidth = 572; // 指定最小分辨率的宽度
1653 int quality = imageQuality(list.length); 1681 int quality = imageQuality(list.length);
  1682 +
  1683 + print(size(list.length));
1654 Uint8List result = await FlutterImageCompress.compressWithList( 1684 Uint8List result = await FlutterImageCompress.compressWithList(
1655 list, 1685 list,
1656 minWidth: minWidth, 1686 minWidth: minWidth,
1657 minHeight: minHeight, 1687 minHeight: minHeight,
1658 quality: quality, 1688 quality: quality,
1659 ); 1689 );
1660 - debugPrint("压缩后图片的大小:${size(result.length)}"); 1690 +
1661 return result; 1691 return result;
1662 } 1692 }
1663 1693
  1694 + // 异步方法,压缩图片到指定大小
  1695 + static Future<Uint8List> compressImage(Uint8List imageBytes,
  1696 + {int quality = 90}) async {
  1697 + try {
  1698 + print("图片初始大小:${imageBytes.length / 1024 / 1024}MB");
  1699 +
  1700 + // 控制压缩后图片大小,不得超过0.5MB
  1701 + // int quality = 90;
  1702 + while (imageBytes.length > 1024 * 1024 * 2 && quality > 0) {
  1703 + imageBytes = await FlutterImageCompress.compressWithList(
  1704 + imageBytes,
  1705 + quality: quality,
  1706 + );
  1707 + print('压缩后大小: ${imageBytes.length / 1024 / 1024}MB');
  1708 + quality -= 2;
  1709 + }
  1710 +
  1711 + return imageBytes;
  1712 + } catch (e) {
  1713 + // 处理异常情况
  1714 + print('压缩图片出错:$e');
  1715 + return null;
  1716 + }
  1717 + }
  1718 +
  1719 + // 异步方法,压缩图片到指定大小
  1720 + static Future<File> compressImageToFile(String path,
  1721 + {int quality = 90}) async {
  1722 + try {
  1723 + File image = File(path);
  1724 + Uint8List imageBytes = await image.readAsBytes();
  1725 + print("图片初始大小:${imageBytes.length / 1024 / 1024}MB");
  1726 +
  1727 + // 控制压缩后图片大小,不得超过0.5MB
  1728 + // int quality = 90;
  1729 + while (imageBytes.length > 1024 * 1024 * 2 && quality > 0) {
  1730 + imageBytes = await FlutterImageCompress.compressWithList(
  1731 + imageBytes,
  1732 + quality: quality,
  1733 + );
  1734 + print('压缩后大小: ${imageBytes.length / 1024 / 1024}MB');
  1735 + quality -= 2;
  1736 + }
  1737 + String imgPath = await saveImage(imageBytes);
  1738 + return File(imgPath);
  1739 + } catch (e) {
  1740 + // 处理异常情况
  1741 + print('压缩图片出错:$e');
  1742 + return null;
  1743 + }
  1744 + }
  1745 +
1664 /// 根据传入的图片字节长度,返回指定的图片质量 1746 /// 根据传入的图片字节长度,返回指定的图片质量
1665 static int imageQuality(int length) { 1747 static int imageQuality(int length) {
1666 debugPrint("压缩前图片的大小:${size(length)}"); 1748 debugPrint("压缩前图片的大小:${size(length)}");
@@ -621,7 +621,42 @@ packages: @@ -621,7 +621,42 @@ packages:
621 name: flutter_image_compress 621 name: flutter_image_compress
622 url: "https://pub.flutter-io.cn" 622 url: "https://pub.flutter-io.cn"
623 source: hosted 623 source: hosted
624 - version: "1.1.3" 624 + version: "2.3.0"
  625 + flutter_image_compress_common:
  626 + dependency: transitive
  627 + description:
  628 + name: flutter_image_compress_common
  629 + url: "https://pub.flutter-io.cn"
  630 + source: hosted
  631 + version: "1.0.5"
  632 + flutter_image_compress_macos:
  633 + dependency: transitive
  634 + description:
  635 + name: flutter_image_compress_macos
  636 + url: "https://pub.flutter-io.cn"
  637 + source: hosted
  638 + version: "1.0.2"
  639 + flutter_image_compress_ohos:
  640 + dependency: transitive
  641 + description:
  642 + name: flutter_image_compress_ohos
  643 + url: "https://pub.flutter-io.cn"
  644 + source: hosted
  645 + version: "0.0.3"
  646 + flutter_image_compress_platform_interface:
  647 + dependency: transitive
  648 + description:
  649 + name: flutter_image_compress_platform_interface
  650 + url: "https://pub.flutter-io.cn"
  651 + source: hosted
  652 + version: "1.0.5"
  653 + flutter_image_compress_web:
  654 + dependency: transitive
  655 + description:
  656 + name: flutter_image_compress_web
  657 + url: "https://pub.flutter-io.cn"
  658 + source: hosted
  659 + version: "0.1.4+1"
625 flutter_inappwebview: 660 flutter_inappwebview:
626 dependency: "direct main" 661 dependency: "direct main"
627 description: 662 description:
@@ -225,7 +225,7 @@ dependencies: @@ -225,7 +225,7 @@ dependencies:
225 extended_text_field: ^10.1.1 225 extended_text_field: ^10.1.1
226 # image_gallery_saver: ^1.7.1 226 # image_gallery_saver: ^1.7.1
227 image_gallery_saver: ^2.0.3 227 image_gallery_saver: ^2.0.3
228 - flutter_image_compress: ^1.1.3 228 + flutter_image_compress: ^2.3.0
229 image: ^4.0.15 229 image: ^4.0.15
230 image_cropper: ^3.0.1 230 image_cropper: ^3.0.1
231 # image_cropper: ^4.0.1 231 # image_cropper: ^4.0.1