我正在尝试在 Flutter 中使用 https.MultipartRequest 调用 API
I am trying to make a call to an API with https.MultipartRequest in Flutter
我正在尝试使用 Flutter 将此 API 用于飞镖:https://pixcut.wondershare.com/api.html。
我的图像作为字符串保存在数据库中,我正在使用方法转换为 string、Uint8list 或 Image 如下所示。我正在尝试使用 http.MultipartRequest 来获取没有背景的图像,但出现此错误:
[log] {"Code":10001,"Msg":"fail msg : http: no such file","Data":null}
void removeBackground(String image) async {
var request = http.MultipartRequest(
"POST", Uri.parse('https://pixcut.wondershare.com/openapi/api/v1/matting/removebg'));
request.headers.addAll(
{
r'Content-Type': 'multipart/form-data',
r'appkey': '061c4600615d101a56330357cafce7d9',
},
);
request.files
.add(http.MultipartFile.fromBytes('content', CleverCloset.dataFromBase64String(image) // use the real name if available, or omit
));
await request.send().then((response) {
http.Response.fromStream(response).then((onValue) {
try {
log(onValue.body);
//stackChildren.add(MoveableStackItem(CleverCloset.imageFromBase64String(onValue.body).image));
log("bb");
setState(() {
});
// get your response here...
} catch (e) {
log(e.toString());
log("ddd");
// handle exeption
}
});
});
}
static Uint8List dataFromBase64String(String base64String) {
return base64Decode(base64String);
}
static String base64String(Uint8List data) {
return base64Encode(data);
}
导入包 dio
https://pub.dev/packages/dio
并将这些代码替换为您自己的代码
Future<String> uploadImage(File file) async {
var dio = Dio();
var url = "https://pixcut.wondershare.com/openapi/api/v1/matting/removebg";
String fileName = file.path.split('/').last;
FormData formData = FormData.fromMap({
"content": await MultipartFile.fromFile(file.path, filename:fileName),
});
var response = await dio.post(url, data: formData,
options: Options(contentType: "multipart/form-data",
headers: {"appkey":"061c4600615d101a56330357cafce7d9"}));
print(response.statusCode);
return response.data;
}
问题是我应该传递一个file.path,所以我将图像保存在一个临时文件中,传递路径,获取字节并使用下面的代码发送字节,现在它的工作方式如下魅力!
Future<void> removeBackground(context, setState, Uint8List image) async {
ProgressDialog pd = ProgressDialog(context: context);
pd.show(max: 100, msg: 'Removing background...',
backgroundColor: const Color(0xff393432),
progressValueColor: const Color(0xff393432),
progressBgColor: const Color(0xffE4BCB4),
msgColor: const Color(0xffE4BCB4),);
final appDir = await getTemporaryDirectory();
File file = File('${appDir.path}/sth.jpg');
await file.writeAsBytes(image);
var headers = {
'x-api-key': '72fe87f131787750e933dcdf80c775fdcf0ad704'
};
var request = http.MultipartRequest('POST', Uri.parse('https://sdk.photoroom.com/v1/segment'));
request.files.add(await http.MultipartFile.fromPath('image_file', file.path));
request.headers.addAll(headers);
var response = await request.send();
if (response.statusCode == 200) {
final List<int> _bytes = [];
response.stream.listen((value) {
_bytes.addAll(value);
}).onDone(() async {
await file.writeAsBytes(_bytes);
pd.close();
setState(() {
var a = CleverCloset.base64String(file.readAsBytesSync());
stackChildren.add(MoveableStackItem(CleverCloset.imageFromBase64String(a).image));
});
});
}
else {
setState(() {
var base64String = CleverCloset.base64String(image);
stackChildren.add(MoveableStackItem(CleverCloset.imageFromBase64String(base64String).image));
});
}
}
我正在尝试使用 Flutter 将此 API 用于飞镖:https://pixcut.wondershare.com/api.html。 我的图像作为字符串保存在数据库中,我正在使用方法转换为 string、Uint8list 或 Image 如下所示。我正在尝试使用 http.MultipartRequest 来获取没有背景的图像,但出现此错误:
[log] {"Code":10001,"Msg":"fail msg : http: no such file","Data":null}
void removeBackground(String image) async {
var request = http.MultipartRequest(
"POST", Uri.parse('https://pixcut.wondershare.com/openapi/api/v1/matting/removebg'));
request.headers.addAll(
{
r'Content-Type': 'multipart/form-data',
r'appkey': '061c4600615d101a56330357cafce7d9',
},
);
request.files
.add(http.MultipartFile.fromBytes('content', CleverCloset.dataFromBase64String(image) // use the real name if available, or omit
));
await request.send().then((response) {
http.Response.fromStream(response).then((onValue) {
try {
log(onValue.body);
//stackChildren.add(MoveableStackItem(CleverCloset.imageFromBase64String(onValue.body).image));
log("bb");
setState(() {
});
// get your response here...
} catch (e) {
log(e.toString());
log("ddd");
// handle exeption
}
});
});
}
static Uint8List dataFromBase64String(String base64String) {
return base64Decode(base64String);
}
static String base64String(Uint8List data) {
return base64Encode(data);
}
导入包 dio https://pub.dev/packages/dio
并将这些代码替换为您自己的代码
Future<String> uploadImage(File file) async {
var dio = Dio();
var url = "https://pixcut.wondershare.com/openapi/api/v1/matting/removebg";
String fileName = file.path.split('/').last;
FormData formData = FormData.fromMap({
"content": await MultipartFile.fromFile(file.path, filename:fileName),
});
var response = await dio.post(url, data: formData,
options: Options(contentType: "multipart/form-data",
headers: {"appkey":"061c4600615d101a56330357cafce7d9"}));
print(response.statusCode);
return response.data;
}
问题是我应该传递一个file.path,所以我将图像保存在一个临时文件中,传递路径,获取字节并使用下面的代码发送字节,现在它的工作方式如下魅力!
Future<void> removeBackground(context, setState, Uint8List image) async {
ProgressDialog pd = ProgressDialog(context: context);
pd.show(max: 100, msg: 'Removing background...',
backgroundColor: const Color(0xff393432),
progressValueColor: const Color(0xff393432),
progressBgColor: const Color(0xffE4BCB4),
msgColor: const Color(0xffE4BCB4),);
final appDir = await getTemporaryDirectory();
File file = File('${appDir.path}/sth.jpg');
await file.writeAsBytes(image);
var headers = {
'x-api-key': '72fe87f131787750e933dcdf80c775fdcf0ad704'
};
var request = http.MultipartRequest('POST', Uri.parse('https://sdk.photoroom.com/v1/segment'));
request.files.add(await http.MultipartFile.fromPath('image_file', file.path));
request.headers.addAll(headers);
var response = await request.send();
if (response.statusCode == 200) {
final List<int> _bytes = [];
response.stream.listen((value) {
_bytes.addAll(value);
}).onDone(() async {
await file.writeAsBytes(_bytes);
pd.close();
setState(() {
var a = CleverCloset.base64String(file.readAsBytesSync());
stackChildren.add(MoveableStackItem(CleverCloset.imageFromBase64String(a).image));
});
});
}
else {
setState(() {
var base64String = CleverCloset.base64String(image);
stackChildren.add(MoveableStackItem(CleverCloset.imageFromBase64String(base64String).image));
});
}
}