flutter 如何在Dart中上传时压缩/缩小图像大小?

1sbrub3j  于 2022-12-14  发布在  Flutter
关注(0)|答案(5)|浏览(304)

如何压缩/减少图像大小,而在 dart 上传?我试图上传一个5 MB的图像,但我想减少其大小,而上传。我正在使用 dart 和Flutter。

ws51t4hk

ws51t4hk1#

有一个image manipulation package on pub。它甚至包括一个例子,确切地说,你想做什么。

import 'dart:io' as Io;
import 'package:image/image.dart';
void main() {
  // Read an image from file (webp in this case).
  // decodeImage will identify the format of the image and use the appropriate
  // decoder.
  Image image = decodeImage(new Io.File('test.webp').readAsBytesSync());

  // Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
  Image thumbnail = copyResize(image, 120);

  // Save the thumbnail as a PNG.
  new Io.File('thumbnail.png')
        ..writeAsBytesSync(encodePng(thumbnail));
}
oyxsuwqo

oyxsuwqo2#

**为了 dart **

使用Image包:
使用这个包,您可以加载图像,调整其大小,并将其保存为png:
首先,在pubspec.yaml文件中添加image作为依赖项。

用法

import 'dart:io';
import 'package:image/image.dart';
void main() {
  // Read an image from file (webp in this case).
  // decodeImage will identify the format of the image and use the appropriate
  // decoder.
  Image image = decodeImage(File('test.webp').readAsBytesSync());

  // Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
  Image thumbnail = copyResize(image, width: 120);

  // Save the thumbnail as a PNG.
  File('thumbnail.png')..writeAsBytesSync(encodePng(thumbnail));
}

如果您想在抖动中使用此图像,请不要使用它。请使用其他解决方案,如**flutter_image_compressResizeImage class**。

:Dart已经有图像压缩库了。为什么要使用本机?
:由于未知原因,Dart语言中的图像压缩效率不高,即使在发布版本中也是如此。使用isolate不能解决问题。

jckbn6z7

jckbn6z73#

你可以尝试使用flutter_native_image包。你可以很容易地压缩图像
示例:

File compressedFile = await FlutterNativeImage.compressImage(file!.path,
    quality: 20, percentage: 60);
qij5mzcb

qij5mzcb4#

我做了这个功能来缩小图片大小,希望对大家有所帮助

import 'package:image/image.dart' as IMG;

 Future<Uint8List> ReduceImageQualityAndSize(Uint8List image) async {
  
  int maxInBytes = 20000;
  Uint8List resizedData = Uint8List.fromList(image);

  IMG.Image? img = await IMG.decodeImage(image);
  int size = await image.lengthInBytes;
  int quality = 100;
  
  print("size max: " + maxInBytes.toString());
  print("size before: " + size.toString() + " bytes");
   
  while (size > maxInBytes && quality > 10) {
     // reduce image size about 10% of image, until the size is less than the maximum limit
    quality = (quality - (quality * 0.1)).toInt();
    int width = img!.width - (img.width * 0.1).toInt();
    IMG.Image resized = await IMG.copyResize(img, width: width);
    resizedData =
      await Uint8List.fromList(IMG.encodeJpg(resized, quality: quality));
    size = await resizedData.lengthInBytes;
    img = resized;
  }

  print("size after: " + size.toString() + " bytes");

  return resizedData;
}

此函数会导致UI冻结,因此使用IsolateWorker时最佳

import 'package:isolated_worker/isolated_worker.dart';

  await IsolatedWorker()
      .run(ReduceImageQualityAndSize, imageUint8List)
      .then((value) {
    imageUint8List = value;
  });
h5qlskok

h5qlskok5#

我过去常常使用dio将文件上传到服务器上
软件包:
flutter_native_image: ^0.0.6+1
dio: ^4.0.6
代码-〉

File? compressedFile = profileImage.value == null
          ? null
          : await FlutterNativeImage.compressImage(
              profileImage.value?.path ?? '',
              quality: 20,
              percentage: 60);

然后再

if (compressedFile != null)
      'profile_pic': await dio.MultipartFile.fromFile(compressedFile.path),

灵感源自**Jordan Kotiadis**

相关问题