图像文件颤动中的图像压缩
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');
}
这是我目前用来保存图像文件的代码。但在保存之前,我希望压缩图像。我找到了使用 image 包的解决方案
但是它的文档本身说它不够快所以我不喜欢使用它。另一个解决方案是使用 image_picker 其示例代码是:
ImagePicker imagePicker = ImagePicker();
PickedFile compressedImage = await imagePicker.getImage(
source: ImageSource.camera,
imageQuality: 85,
);
我尝试使用相同的示例,但源属性的值不是 File,因此我无法进行压缩。
请提出一些解决方案来完成这项工作。
使用这个库:Flutter Image Compress
将图像压缩为本机插件 (Obj-C/Kotlin)
或者未知原因,Dart 语言中的图像压缩效率不高,即使在发布版本中也是如此。
谢谢,@Narendra_Nath,你的回答很有用。
后来,我遇到了这个解决方案,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.
The flutter_image_compress package has a required attribute i.e. output path which was to be determined forehand, it felt quite overwhelming for me so, I used the flutter_native_image包。
查看此答案前面提到的解决方案,以了解有关这 3 个软件包的更多信息。
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');
}
这是我目前用来保存图像文件的代码。但在保存之前,我希望压缩图像。我找到了使用 image 包的解决方案 但是它的文档本身说它不够快所以我不喜欢使用它。另一个解决方案是使用 image_picker 其示例代码是:
ImagePicker imagePicker = ImagePicker();
PickedFile compressedImage = await imagePicker.getImage(
source: ImageSource.camera,
imageQuality: 85,
);
我尝试使用相同的示例,但源属性的值不是 File,因此我无法进行压缩。 请提出一些解决方案来完成这项工作。
使用这个库:Flutter Image Compress
将图像压缩为本机插件 (Obj-C/Kotlin)
或者未知原因,Dart 语言中的图像压缩效率不高,即使在发布版本中也是如此。
谢谢,@Narendra_Nath,你的回答很有用。
后来,我遇到了这个解决方案,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.
The flutter_image_compress package has a required attribute i.e. output path which was to be determined forehand, it felt quite overwhelming for me so, I used the flutter_native_image包。
查看此答案前面提到的解决方案,以了解有关这 3 个软件包的更多信息。