我有这个代码可以在颤振中居中裁剪图像:
Future loadImage() async {
final imageBytes = File(imageFilePath).readAsBytesSync();
final newImage = img.decodeImage(imageBytes);
var cropSize = min(newImage.width, newImage.height);
print(cropSize.toString() + " cropsizeeeeee");
int offsetX = (newImage.width - min(newImage.width, newImage.height)) ~/ 2;
int offsetY = (newImage.height - min(newImage.width, newImage.height)) ~/ 2;
print(offsetX.toString() + " offsetXxxxxxxxxx");
print(offsetY.toString() + " offsetYyyyyyyyyyyyyyyy");
img.Image destImage = img.copyCrop(newImage, offsetX, offsetY, cropSize, cropSize);
if (widget.flip) {
destImage = img.flipHorizontal(destImage);
}
if (widget.crop) {
setState(() {
this.image = destImage;
});
saveImage(destImage);
} else {
setState(() {
this.image = newImage;
});
saveImage(newImage);
}
]
}
但由于项目要求的变化,我想从顶部裁剪图像。我无法计算 x 和 y 偏移量以从顶部获取图像。如何从顶部裁剪图像?