簡體   English   中英

如何從 flutter 中的 image_cropper 獲取路徑?

[英]how to get the path from image_cropper in flutter?

我想將裁剪后的圖像上傳到服務器中,但我不知道如何從裁剪器中獲取圖像路徑。 如何從裁剪器中獲取路徑。 下面是我的裁剪和上傳裁剪圖像的代碼。

這是我的裁剪代碼。

    `void _cropImage(filePath) async {
CroppedFile? _croppedFile = await ImageCropper().cropImage(
  sourcePath: filePath,
  aspectRatioPresets: [
    CropAspectRatioPreset.square,
    CropAspectRatioPreset.ratio3x2,
    CropAspectRatioPreset.original,
    CropAspectRatioPreset.ratio4x3,
    CropAspectRatioPreset.ratio16x9
  ],
  uiSettings: [
    AndroidUiSettings(
        toolbarTitle: 'Cropper',
        toolbarColor: Colors.deepOrange,
        toolbarWidgetColor: Colors.white,
        initAspectRatio: CropAspectRatioPreset.original,
        lockAspectRatio: false),
    IOSUiSettings(
      title: 'Cropper',
    ),
  ],);
//     compressFormat: ImageCompressFormat.jpg);
// cropImagePath.value = croppedFile!.path;
if (_croppedFile != null) {
  setState(() {
    imageFile = _croppedFile.path;
  });
}

}

這是我的上傳代碼

    `uploadImage() async {
var request = http.MultipartRequest(
    'POST', Uri.parse('http://hsdgfddf/api/examples/add'));
request.files.add(await http.MultipartFile.fromPath(
    'picture', croppedfile!.path));

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

}

收到修改后的裁剪圖像后,您可以在設置時獲取圖像路徑。

希望這可以幫助。

嘗試這個

var request = http.MultipartRequest('POST', Uri.parse("API"), );

Map<String, String> headers = {"Content-type": "multipart/form-data"};

request.files.add(
  http.MultipartFile(
    'pic' //picture_index,
    selectedImage.readAsBytes().asStream(),
    selectedImage.lengthSync(),
    filename: croppedImage.path.split('/').last,
  ),
);

request.headers.addAll(headers);
print("request: " + request.toString());
var res = await request.send();

http.Response response = await http.Response.fromStream(res);

暫無
暫無

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

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