簡體   English   中英

Flutter 我試圖將圖像裁剪/調整為 1:1 比例,但在使用 image_cropper 時出現問題 package

[英]Flutter i trying to crop/resize image into 1:1 ratio but i got a problem when i use image_cropper package

我正在嘗試裁剪從畫廊檢索的圖像或拍照,我需要將圖像調整為 1:1 大小並將其上傳到 firebase

這是我已經嘗試過的代碼,但我遇到了一些錯誤

 Future<Null> _cropImage(File image) async {
    File? croppedImage = await ImageCropper.cropImage(
        sourcePath: image.path,
        maxWidth: 1080,
        maxHeight: 1080,
        aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
    );
    if (croppedImage  != null) {
        imageFile = croppedImage ;
        setState(() {});
    }
  }

這是我用來獲取圖像和裁剪圖像的代碼

下面是我用來毫無問題地拍攝圖像的原始代碼

Future _getCameraImage() async {
    final image = await ImagePicker().pickImage(
        source: ImageSource.camera,
        imageQuality: 40,
        maxHeight: 800,
        maxWidth: 800);

    setState(() {
      _image = File(image!.path);
    });
  } 

這是我從 flutter 得到的錯誤

當我將 _getCameraImage 放在我的 TextButton.icon 中時,像這樣按下。 他們什么都沒給我

TextButton.icon(
                            onPressed: () => _getCameraImage,
                            icon: const Icon(Icons.camera_alt),
                            label: const Text('Camera'),
                          ),

我想從相機獲取圖像並將其顯示在我的 imageview 上

在此處輸入圖像描述

我認為圖像裁剪器庫的自述文件沒有正確更新,或者從他們的角度來看這是一個錯誤,這就是為什么他們顯示ImageCropper.cropImage而不是ImageCropper().cropImage的原因。

根據圖像裁剪器庫示例,您必須先創建圖像裁剪器 object 才能使用裁剪機制,因為cropImage不是 static 方法。

所以嘗試使用ImageCropper().cropImage

 Future<Null> _cropImage(File image) async {
    File? croppedImage = await ImageCropper().cropImage(
        sourcePath: image.path,
        maxWidth: 1080,
        maxHeight: 1080,
        aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
    );
    if (croppedImage  != null) {
        imageFile = croppedImage ;
        setState(() {});
    }
  }

暫無
暫無

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

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