Przeglądaj źródła

压缩图片增加高度限制。

PC\19500 2 tygodni temu
rodzic
commit
78203515a1
2 zmienionych plików z 34 dodań i 4 usunięć
  1. 25 4
      lib/media/image_processor.dart
  2. 9 0
      lib/media/media_service.dart

+ 25 - 4
lib/media/image_processor.dart

@@ -8,6 +8,9 @@ class ImageProcessor {
   /// 默认压缩宽度(医疗场景推荐)
   static const int defaultMaxWidth = 1080;
   
+  /// 默认压缩高度(医疗场景推荐)
+  static const int defaultMaxHeight = 1920;
+  
   /// 默认压缩质量(85% 平衡质量和体积)
   static const int defaultQuality = 85;
 
@@ -17,12 +20,14 @@ class ImageProcessor {
   /// 
   /// - [file] 原始图片文件
   /// - [maxWidth] 最大宽度,默认 1080px
+  /// - [maxHeight] 最大高度,默认 1920px
   /// - [quality] 压缩质量(1-100),默认 85
   /// 
   /// 返回压缩后的文件(覆盖原文件)
   static Future<File> compress(
     File file, {
     int maxWidth = defaultMaxWidth,
+    int? maxHeight,
     int quality = defaultQuality,
   }) async {
     try {
@@ -40,17 +45,33 @@ class ImageProcessor {
         throw ImageProcessingException('无法解析图片: ${file.path}');
       }
 
-      // 如果图片宽度小于等于最大宽度,只进行质量压缩
+      // 使用默认最大高度(如果未指定)
+      final effectiveMaxHeight = maxHeight ?? defaultMaxHeight;
+
+      // 计算是否需要缩放
+      final needsResize = image.width > maxWidth || image.height > effectiveMaxHeight;
+      
       File? resultFile;
-      if (image.width <= maxWidth) {
+      if (!needsResize) {
+        // 如果图片尺寸都在限制内,只进行质量压缩
         final compressed = img.encodeJpg(image, quality: quality);
         resultFile = File(file.path)..writeAsBytesSync(compressed);
       } else {
+        // 计算缩放比例,保持宽高比
+        final widthRatio = image.width / maxWidth;
+        final heightRatio = image.height / effectiveMaxHeight;
+        final ratio = widthRatio > heightRatio ? widthRatio : heightRatio;
+        
+        // 计算新尺寸
+        final newWidth = (image.width / ratio).round();
+        final newHeight = (image.height / ratio).round();
+        
         // 先缩放再压缩
         final resized = img.copyResize(
           image,
-          width: maxWidth,
-          maintainAspect: true,
+          width: newWidth,
+          height: newHeight,
+          maintainAspect: false, // 我们已经计算好了尺寸
         );
         final compressed = img.encodeJpg(resized, quality: quality);
         resultFile = File(file.path)..writeAsBytesSync(compressed);

+ 9 - 0
lib/media/media_service.dart

@@ -30,6 +30,7 @@ class MediaService {
   /// 
   /// - [imageQuality] 图片质量(1-100),默认 90
   /// - [maxWidth] 压缩后的最大宽度,默认 1080px
+  /// - [maxHeight] 压缩后的最大高度,默认 1920px
   /// - [compressQuality] 压缩质量(1-100),默认 85
   /// 
   /// 返回压缩后的照片文件,如果用户取消或失败返回 null
@@ -38,6 +39,7 @@ class MediaService {
   static Future<File?> pickFromCamera({
     int imageQuality = PickerService.defaultImageQuality,
     int maxWidth = ImageProcessor.defaultMaxWidth,
+    int? maxHeight,
     int compressQuality = ImageProcessor.defaultQuality,
   }) async {
     try {
@@ -54,6 +56,7 @@ class MediaService {
       final compressed = await ImageProcessor.compress(
         file,
         maxWidth: maxWidth,
+        maxHeight: maxHeight,
         quality: compressQuality,
       );
 
@@ -76,6 +79,7 @@ class MediaService {
   /// 
   /// - [imageQuality] 图片质量(1-100),默认 90
   /// - [maxWidth] 压缩后的最大宽度,默认 1080px
+  /// - [maxHeight] 压缩后的最大高度,默认 1920px
   /// - [compressQuality] 压缩质量(1-100),默认 85
   /// 
   /// 返回压缩后的图片文件,如果用户取消或失败返回 null
@@ -84,6 +88,7 @@ class MediaService {
   static Future<File?> pickFromGallery({
     int imageQuality = PickerService.defaultImageQuality,
     int maxWidth = ImageProcessor.defaultMaxWidth,
+    int? maxHeight,
     int compressQuality = ImageProcessor.defaultQuality,
   }) async {
     try {
@@ -100,6 +105,7 @@ class MediaService {
       final compressed = await ImageProcessor.compress(
         file,
         maxWidth: maxWidth,
+        maxHeight: maxHeight,
         quality: compressQuality,
       );
 
@@ -120,6 +126,7 @@ class MediaService {
   /// 
   /// - [file] 原始图片文件
   /// - [maxWidth] 最大宽度,默认 1080px
+  /// - [maxHeight] 最大高度,默认 1920px
   /// - [quality] 压缩质量(1-100),默认 85
   /// 
   /// 返回压缩后的文件(覆盖原文件)
@@ -128,11 +135,13 @@ class MediaService {
   static Future<File> compressImage(
     File file, {
     int maxWidth = ImageProcessor.defaultMaxWidth,
+    int? maxHeight,
     int quality = ImageProcessor.defaultQuality,
   }) async {
     return await ImageProcessor.compress(
       file,
       maxWidth: maxWidth,
+      maxHeight: maxHeight,
       quality: quality,
     );
   }