我要求用户裁剪他从图库中选择的任何图像,图像的纵横比为1/1,然后我意识到图像包也可以在不询问用户的情况下裁剪,但是我不知道如何计算纵横比,如果它是1/1,以及如何将这些值传递给image.copycrop(int x,int y,int高度,int宽度)!
我用这个代码来裁剪
final croppedImage = await ImageCropper().cropImage(
sourcePath: images!.path,
cropStyle: CropStyle.rectangle,
compressQuality: 100,
compressFormat: ImageCompressFormat.jpg,
aspectRatio: const CropAspectRatio(ratioX: 0.8, ratioY: 1.0),现在我发现我可以使用图像包进行裁剪,但是我不知道如何插入像cropasepectratio这样的值(0.8,1.0)。
var decodedImage = await decodeImageFromList(File(images!.path).readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height);
final imageBytes =
decodeImage(File(images!.path).readAsBytesSync())!;
img.Image cropOne = img.copyCrop(
imageBytes,
100,
100,
decodedImage.width,
decodedImage.height,
);
File(images!.path).writeAsBytes(encodePng(cropOne));发布于 2022-06-16 12:28:00
这是解决作物和保持高宽比为1/1的解决方案,它将图像精确地放在中间。
var decodedImage = await decodeImageFromList(File(images!.path).readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height);
var cropSize = min(decodedImage.width, decodedImage.height);
int offsetX = (decodedImage.width - min(decodedImage.width, decodedImage.height)) ~/ 2;
int offsetY = (decodedImage.height - min(decodedImage.width, decodedImage.height)) ~/ 2;
final imageBytes = decodeImage(File(images!.path).readAsBytesSync())!;
img.Image cropOne = img.copyCrop(
imageBytes,
offsetX,
offsetY,
cropSize,
cropSize,
);
print(cropOne.height);
print(cropOne.width);
File(images!.path).writeAsBytes(encodePng(cropOne));https://stackoverflow.com/questions/72644796
复制相似问题