简体   繁体   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

im trying to crop an image that i retrieve from gallery or take a photo and i need to resize the image into 1:1 size and upload it to firebase我正在尝试裁剪从画廊检索的图像或拍照,我需要将图像调整为 1:1 大小并将其上传到 firebase

this is the code that i already try but i got some error这是我已经尝试过的代码,但我遇到了一些错误

 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(() {});
    }
  }

this is the code that i use to get image and crop the image这是我用来获取图像和裁剪图像的代码

down below is my original code that i used to take image without no problem下面是我用来毫无问题地拍摄图像的原始代码

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

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

and this is the error i got from flutter这是我从 flutter 得到的错误

and when i put the _getCameraImage in my TextButton.icon onpressed like this.当我将 _getCameraImage 放在我的 TextButton.icon 中时,像这样按下。 they didt gave me anything他们什么都没给我

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

i want to get image from camera and display it on my imageview我想从相机获取图像并将其显示在我的 imageview 上

在此处输入图像描述

I think image cropper library's read me file is not updated properly or it was an mistake from their end, thats why they show ImageCropper.cropImage instead of ImageCropper().cropImage .我认为图像裁剪器库的自述文件没有正确更新,或者从他们的角度来看这是一个错误,这就是为什么他们显示ImageCropper.cropImage而不是ImageCropper().cropImage的原因。

As per image cropper library example, you have to create the image cropper object first to use the cropping mechanism because cropImage is not an static method.根据图像裁剪器库示例,您必须先创建图像裁剪器 object 才能使用裁剪机制,因为cropImage不是 static 方法。

So try to use ImageCropper().cropImage所以尝试使用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