【发布时间】:2021-09-01 18:55:53
【问题描述】:
我有一个屏幕,可以让用户导入图像并拖放它们以重新定位、调整大小和旋转它们。每个对象都是一个小部件,所有这些对象的父级是一个Stack 小部件。我使用matrix_gesture_detector 0.1.0 库来处理转换。我使用screenshot 库将这个Stack 小部件保存为图像。但是,如果pixelRatio 低,我会得到像素化图像,但如果它高,图像大小会增加很多(大约 25MB)。我想知道如何在不增加图像大小的情况下提高图像质量。如果还有其他方法,我也想了解它们。我的图片可以是AssetImage 或NetWorkImage。
我的截图代码:
....
....
Screenshot(
controller: screenshotController,
child: Container(
key: Key("canvas"),
decoration: BoxDecoration(
color: currentColor,
borderRadius: BorderRadius.all(
Radius.circular(dp2),
),
),
child: Stack(
alignment: Alignment.center,
children: _selectedWidgets, // here selected widgets is a lis of widgets
),
),
)
//save image method
saveImage() async {
String fileName = DateTime.now().toIso8601String();
var path = '$directoryPath/$fileName.png';
screenshotController
.capture(
pixelRatio: 5,
path: path,
delay: Duration(milliseconds: 10),
).then((File image) async {
image = await compressImage(image);
........
.........
}).catchError((onError, stack) {
log("stack = $stack");
});
}
// compressing function compress based on the size of the image
Future<File> compressImage(File image) async {
int fileSize = image.lengthSync();
int percentage = getPercentage(fileSize);
return FlutterNativeImage.compressImage(image.path, quality: 25, percentage: percentage);
}
int getPercentage(int fileSize) {
if(fileSize <= 1073741824)
return 75;
if(fileSize <= 2147483648)
return 60;
if(fileSize <= 3221225472)
return 50;
if(fileSize <= 4294967296)
return 40;
if(fileSize <= 5368709120)
return 25;
return 25;
}
【问题讨论】:
标签: image flutter screenshot