簡體   English   中英

如何使用圖像包“image.copyCrop 方法”以縱橫比 1/1 裁剪圖像

[英]How to crop the image using the image package "image.copyCrop method" in aspect ratio 1/1

我要求用戶使用縱橫比為 1/1 的圖像裁剪器裁剪他從圖庫中選擇的任何圖像,然后我意識到圖像包也可以在不詢問用戶的情況下裁剪,但我不知道如何計算縱橫比比率(如果是 1/1)以及如何將這些值傳遞給 image.copycrop(int x, int y, int height,int width) !

我正在使用此代碼進行裁剪

 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),

現在我發現我可以使用圖像包進行裁剪,但我不知道如何插入像cropasespectratio(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));

這是裁剪並保持縱橫比為 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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM