如何使用图像软件包裁剪图像。纵横比1/1

发布于 2025-02-08 04:48:57 字数 1027 浏览 0 评论 0原文

我要用户用图像替纸夹以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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2025-02-15 04:48:57

这是作物的解决方案,并保持长宽比为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));

This is the solution to crop and keep the aspect ratio of 1/1. It crops the image exactly in the middle.

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文