gpt4 book ai didi

flutter - 如何在Flutter中使用multi_image_picker调整图像大小?

转载 作者:行者123 更新时间:2023-12-03 03:33:53 28 4
gpt4 key购买 nike

我正在使用multi_image_picker包来选择图像并上传到服务器,但是在上传之前,我想调整图像的大小。我正在尝试使用dart.ui完成此操作,但遇到了问题:

//assets is List<Asset> from MultiImagePicker.pickImages
assets.forEach((asset) {
Future<ByteData> byteData = asset.getByteData();
byteData.then((d) async {
List<int> imageData = d.buffer.asUint8List();
String b64 =base64Encode(imageData);
print(b64); // prints [/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE...
//if i send b64 to server then decode it and save as img it's working well

//Resize
ui.instantiateImageCodec(imageData,targetHeight: 800, targetWidth: 600)
.then((codec) {
codec.getNextFrame().then((frameInfo) async {
ui.Image i = frameInfo.image;
ByteData bytes = await i.toByteData();
List<int> resizedImageData = bytes.buffer.asUint8List();
String rb64 = base64Encode(resizedImageData);
print(rb64); // prints too many backslashes:[k5KO/5qWk/+ZlZL/mpaT/5uXlP+alpP/mJSR/5iUkf+YlJH/mZSR/5uWk/+blpP/n5qX/6GcmP+gm5f/oZyY/6GcmP+fmpb/nZi..
//If i send rb64 to server then server cannot decode and save it.
});
});
});
});

最佳答案

这是我通常用来调整大小的函数:

import 'dart:ui' as skia;

Future<skia.Image> resizeImage(String path, {int width, int height}) async {
Uint8List data = await File(path).readAsBytes();
final codec = await skia.instantiateImageCodec(data, targetWidth: width, targetHeight: height);
final frame = await codec.getNextFrame();
return frame.image;
}

正如我在评论中提到的那样,当前在Flutter Web中不起作用,但这是由于希望尽快修复此错误。

关于flutter - 如何在Flutter中使用multi_image_picker调整图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62188074/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com