imageCompressAndGetFile static method

Future<Object?> imageCompressAndGetFile({
  1. File? file,
  2. String? path,
})

图片压缩 File -> File

Implementation

static Future<Object?> imageCompressAndGetFile(
    {File? file, String? path}) async {
  if (file!.lengthSync() < 200 * 1024) {
    return file;
  }
  var quality = 100;
  if (file.lengthSync() > 6 * 1024 * 1024) {
    quality = 50;
  } else if (file.lengthSync() > 4 * 1024 * 1024) {
    quality = 60;
  } else if (file.lengthSync() > 2 * 1024 * 1024) {
    quality = 70;
  } else if (file.lengthSync() > 1 * 1024 * 1024) {
    quality = 80;
  } else if (file.lengthSync() > 0.5 * 1024 * 1024) {
    quality = 90;
  } else if (file.lengthSync() > 0.25 * 1024 * 1024) {
    quality = 100;
  }
  var dir = await path_provider.getTemporaryDirectory();
  var targetPath =
      "${dir.absolute.path}/${DateTime.now().millisecondsSinceEpoch}.jpg";

  var result = await FlutterImageCompress.compressAndGetFile(
    file.absolute.path,
    path ?? targetPath,
    minWidth: 600,
    quality: quality,
    rotate: 0,
  );
  return result;
}