removeBackground static method

Future<LocalRembgResultModel> removeBackground({
  1. String? imagePath,
  2. Uint8List? imageUint8List,
  3. bool? cropTheImage = true,
})

Removes the background from the specified image file. Returns a LocalRembgResultModel representing the result of the operation. imagePath Throws an error if the provided image path '.png', '.jpg', '.jpeg', '.heic' is invalid or unsupported. imageUint8List (Requires iOS platform). cropTheImage Specifies whether to crop the segmented image after removing the background. If set to true, the segmented image will be cropped to remove any transparent or empty areas. If set to false, the segmented image will be returned without any cropping.

Implementation

static Future<LocalRembgResultModel> removeBackground({
  String? imagePath,
  Uint8List? imageUint8List,
  bool? cropTheImage = true,
}) async {
  if (imagePath == null && imageUint8List == null) {
    return LocalRembgResultModel(
      status: 0,
      imageBytes: null,
      errorMessage:
          "You must provide either 'imagePath' or 'imageUint8List'.",
    );
  }

  if (imagePath != null && !imagePath.typeIsImage) {
    return LocalRembgResultModel(
      status: 0,
      imageBytes: null,
      errorMessage: 'Invalid image type!',
    );
  }

  if (Platform.isAndroid && imageUint8List != null) {
    return LocalRembgResultModel(
      status: 0,
      imageBytes: null,
      errorMessage: 'imageUint8List is supported only on iOS platform.',
    );
  }

  Map<dynamic, dynamic> methodChannelResult = await _channel.invokeMethod(
    'removeBackground',
    {
      'imagePath': imagePath,
      'imageUint8List': imageUint8List,
      'cropImage': cropTheImage,
    },
  );
  if (kDebugMode) {
    print(methodChannelResult);
  }
  return LocalRembgResultModel.fromMap(
    methodChannelResult,
  );
}