有什么方法可以从 Flutter 中的图像中删除背景?

Is there any way to remove background from an image in Flutter?

我对此进行了很多搜索,但没有找到任何地方。有一些 API,但它们在 50-100 张图像后都有定价模式,例如 https://www.remove.bg

是否有来自 PhotoshopLasso Tool 插件?或者免费 API?

2 天后,我发现唯一的 API 每个月提供 1000 次免费 API 通话,之后如果您放置他们的徽标,则每次请求收取 0.01 美元。 它就像一个魅力:https://dashboard.photoroom.com. You have to create an account, after that you'll get the API Key. The content, header and all the details for the implementation you will find here: https://www.notion.so/photoroom/API-Documentation-public-4eb3e45d9c814f92b6392b7fd0f1d51f.

我在下面使用了这段代码:

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': '72fe87f131787350e933dcdf80c775fdcf0ad704'
    };
    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));
      });
    }
  }