removeBackground method

Future<String> removeBackground(
  1. String imagePath, {
  2. bool cropToSubject = true,
})

Implementation

Future<String> removeBackground(
  String imagePath, {
  bool cropToSubject = true,
}) async {
  if (imagePath.trim().isEmpty || !File(imagePath).existsSync()) {
    return imagePath;
  }

  try {
    final result = await NativeCutout.removeBackground(
      imagePath,
      options: CutoutOptions(
        cropToSubject: cropToSubject,
        writeToCache: true,
      ),
    );

    if (result is CutoutFileSuccess) return result.path;

    if (result is CutoutBytesSuccess) {
      return _writeBytesToTemp(result.pngBytes);
    }

    if (result is CutoutFailure) {
      CloudLogger.warning(
        'Background removal failed: ${result.code.name} - ${result.message}',
      );
    }
  } catch (e, st) {
    CloudLogger.error(
      'Background removal failed',
      error: e,
      stackTrace: st,
    );
  }

  return imagePath;
}