如何使用图像软件包裁剪图像。纵横比1/1
我要用户用图像替纸夹以1/1的形象裁剪他从画廊中选择的任何图像,然后我意识到,图像包也可以裁剪,甚至不问用户,但我不知道如何计算方面比率是1/1,以及如何将这些值传递给图像。
我现在正在使用此代码进行裁剪,
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),
我发现我可以用图像包进行裁剪,但我不知道如何插入诸如propasepectratio之类的值(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));
i'm asking the user to crop any image he choose from the gallery with the image cropper with aspect ratio 1/1 then i realize that image package can also crop without even asking the user but i don't know how to calculate the aspect ratio if it is 1/1 and how to pass these values to the image.copycrop(int x, int y, int height,int width) !
i was using this code to crop
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),
now i figured out that i can crop with image package but i don't know how to insert the values like the 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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是作物的解决方案,并保持长宽比为1/1。它恰好在中间播种图像。
This is the solution to crop and keep the aspect ratio of 1/1. It crops the image exactly in the middle.