首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用长径比为1/1的图像包"image.copyCrop方法“裁剪图像

如何使用长径比为1/1的图像包"image.copyCrop方法“裁剪图像
EN

Stack Overflow用户
提问于 2022-06-16 11:03:07
回答 1查看 417关注 0票数 0

我要求用户裁剪他从图库中选择的任何图像,图像的纵横比为1/1,然后我意识到图像包也可以在不询问用户的情况下裁剪,但是我不知道如何计算纵横比,如果它是1/1,以及如何将这些值传递给image.copycrop(int x,int y,int高度,int宽度)!

我用这个代码来裁剪

代码语言:javascript
复制
 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)。

代码语言:javascript
复制
 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));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-16 12:28:00

这是解决作物和保持高宽比为1/1的解决方案,它将图像精确地放在中间。

代码语言:javascript
复制
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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72644796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档