我有一个屏幕,可以让用户导入图像并拖放它们以重新定位、调整大小和旋转它们。每个对象都是一个小部件,所有这些对象的父对象都是一个
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;
}
那么您的应用程序的整个目标是创建图像拼贴并将其保存到磁盘? (如果你先解释一下可能会有用)
我建议要么限制“画布”区域的大小,要么在保存之前调整它的大小。您还可以尝试各种压缩算法,例如 JPEG 及其参数。不确定质量如何
FlutterNativeImage
。也许你可以看看其他一些包?我看到 flutter_image_compress 包有很多人喜欢。
你找到解决方案了吗,因为我也遇到了同样的问题。