简体   繁体   English

图像文件 flutter 中的图像压缩

[英]Image Compression in flutter of a Image File

Future<void> saveLocalStorage(File image) async {

// get file format of the image
String fileFormat = image.path.split('.').last; // jpg

// get path for applications directory
final directory = await getApplicationDocumentsDirectory();

// January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch.
int time = DateTime.now().millisecondsSinceEpoch;

// saving image into applications document directory
image.copy('${directory.path}/$time.$fileFormat');
}

This is the code that I am currently using to save an image File.这是我目前用来保存图像文件的代码。 But before saving, I wish to compress the Image.但在保存之前,我希望压缩图像。 I found a solution using the image package but its documentation itself says that it isn't fast enough so I don't prefer using it.我找到了一个使用图像package 的解决方案,但它的文档本身说它不够快,所以我不喜欢使用它。 Another solution was using the image_picker whose example code is:另一个解决方案是使用image_picker ,其示例代码是:

ImagePicker imagePicker = ImagePicker();
PickedFile compressedImage = await imagePicker.getImage(
  source: ImageSource.camera,
  imageQuality: 85,
);

I tried using the same example but the source attribute doesn't File as its value so I am unable to do the compression.我尝试使用相同的示例,但 source 属性没有 File 作为其值,因此我无法进行压缩。 Please suggest some solution to get this done.请提出一些解决方案来完成这项工作。

Use this library: Flutter Image Compress使用这个库: Flutter Image Compress

Compresses image as native plugin (Obj-C/Kotlin)将图像压缩为原生插件 (Obj-C/Kotlin)

or unknown reasons, image compression in Dart language is not efficient, even in release version.或未知原因,Dart 语言中的图像压缩效率不高,即使在发布版本中也是如此。

Thanks, @Narendra_Nath, ur answer was quite useful.谢谢,@Narendra_Nath,您的回答非常有用。
Later, I came across this solution, Flutter the best way to compress image it explained 3 packages, one of which was the flutter_image_compress package, it was suitable for my case but I preferred using the flutter_native_image package as it had easy to understand syntax.后来,我遇到了这个解决方案, Flutter 压缩图像的最佳方式它解释了 3 个包,其中之一是flutter_image_compress package,它适合我的情况,但我更喜欢使用flutter_native_image package,因为它的语法易于理解。
The flutter_image_compress package has a required attribute ie output path which was to be determined forehand, it felt quite overwhelming for me so, I used the flutter_native_image package. flutter_image_compress package 有一个必需的属性,即 output 路径,这是要预先确定的,这对我来说感觉很困难,所以我使用了flutter_native_image package。
Check the solution mentioned earlier in this answer to know more about those 3 packages.检查此答案前面提到的解决方案,以了解有关这 3 个包的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM